函数 'cvSetMouseCallback' 中的 NULL window 处理程序

NULL window handler in function 'cvSetMouseCallback'

我正在做一个项目,我必须将鼠标用作画笔。我使用了 cv2.setMouseCallback() 函数,但它返回了以下错误。 这是我的代码的一部分

import cv2
import numpy as np

# mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(255,0,0),-1)

# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

当我 运行 它返回给我以下错误: 错误回溯

(most recent call last)
<ipython-input-1-640e54baca5f> in <module>
     10 img = np.zeros((512,512,3), np.uint8)
     11 cv2.namedWindow('image')
---> 12 cv2.setMouseCallback('image',draw_circle)
     13 
     14 while(1):

error: OpenCV(4.3.0) /io/opencv/modules/highgui/src/window_QT.cpp:717: error: (-27:Null pointer) NULL window handler in function 'cvSetMouseCallback'

我的 python 版本 - 3.8 操作系统 - ubuntu 20

错误解决我把之前安装的OpenCV删除了

(使用 pip pip install opencv-python 安装)并使用

重新安装

sudo apt install libopencv-dev python3-opencv

对我来说,还使用了 pip3 安装。 刚刚创建了一个新的虚拟环境,并安装了 opencv-python 和 opencv-contrib-python.