You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PyPi version 4.3.0.36 sometimes crashes on cv2.setMouseCallback #356
On 4.3.0.36 version, sometimes after step 2, OpenCV crashes:
cv2.error: OpenCV(4.3.0) /io/opencv/modules/highgui/src/window_QT.cpp:717: error: (-27:Null pointer) NULL window handler in function 'cvSetMouseCallback'.
Note that this symptom does not occur on version 4.2.0.34.
Steps to reproduce
example code
# Code from official docs: https://docs.opencv.org/master/db/d5b/tutorial_py_mouse_handling.htmlimportnumpyasnpimportcv2ascv# mouse callback functiondefdraw_circle(event,x,y,flags,param):
ifevent==cv.EVENT_LBUTTONDBLCLK:
cv.circle(img,(x,y),100,(255,0,0),-1)
# Create a black image, a window and bind the function to windowimg=np.zeros((512,512,3), np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):
cv.imshow('image',img)
ifcv.waitKey(20) &0xFF==27:
breakcv.destroyAllWindows()
I think this is some kind of race condition in OpenCV code or elsewhere. OpenCV (or Qt) seems to open randomly some other window or something before the actual named window appears. Very odd behavior... I have no time to deep dive into the issue, so here is a short workaround which seems to work reliably and should get you going:
importnumpyasnpimportcv2window_name="test"defdraw_circle(event,x,y,flags,param):
ifevent==cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
img=np.zeros((512,512,3), np.uint8)
window_open=False# just try as long as a matching window is foundwhilenotwindow_open:
try:
cv2.namedWindow(window_name)
cv2.setMouseCallback(window_name, draw_circle)
window_open=Trueexcept:
# destroy any "erroneous" windows OpenCV might have createdcv2.destroyAllWindows()
while(1):
cv2.imshow(window_name, img)
ifcv2.waitKey(20) &0xFF==27:
breakcv2.destroyAllWindows()
Other alternative is to use some actual Qt wrapper (i.e. PyQt) / GUI toolkit to create UIs. OpenCV's GUI functionality is quite limited and imho useful only for debugging purposes.
I agree on try-except work-around, as an alternative to using another version, like opencv-python==4.2.0.34.
As for the practicality of HighGUI, note that it's being built upon by some larger-scale projects in the community. Notably, I encountered this github issue originally when trying to make GibsonV2 work. I personally would also use PyQt instead, but I can understand the developers' wish to go fast on their main contributions (a robot simulation environment) and delegate responsibility to other libraries.
This issue might get solved if I manage to update the Linux wheels to Qt5. As there are workarounds, I'm not planning to debug this further unless someone points me directly to the root cause. I'll keep the issue open so I remember to test it in the future in new versions.
Activity
skvark commentedon Jul 7, 2020
The base
manylinuximage was changed between those versions. Qt version is the same 4.8.7.skvark commentedon Jul 7, 2020
I think this is some kind of race condition in OpenCV code or elsewhere. OpenCV (or Qt) seems to open randomly some other window or something before the actual named window appears. Very odd behavior... I have no time to deep dive into the issue, so here is a short workaround which seems to work reliably and should get you going:
Other alternative is to use some actual Qt wrapper (i.e. PyQt) / GUI toolkit to create UIs. OpenCV's GUI functionality is quite limited and imho useful only for debugging purposes.
anqixu commentedon Jul 7, 2020
I agree on try-except work-around, as an alternative to using another version, like
opencv-python==4.2.0.34.As for the practicality of HighGUI, note that it's being built upon by some larger-scale projects in the community. Notably, I encountered this github issue originally when trying to make GibsonV2 work. I personally would also use PyQt instead, but I can understand the developers' wish to go fast on their main contributions (a robot simulation environment) and delegate responsibility to other libraries.
skvark commentedon Jul 7, 2020
This issue might get solved if I manage to update the Linux wheels to Qt5. As there are workarounds, I'm not planning to debug this further unless someone points me directly to the root cause. I'll keep the issue open so I remember to test it in the future in new versions.
skvark commentedon Jul 28, 2020
Closing, please see #362