Skip to content

PyPi version 4.3.0.36 sometimes crashes on cv2.setMouseCallback #356

Closed

Description

@anqixu

opencv/opencv#17767

Expected behaviour

  1. create named window
  2. set mouse callback
  3. program keep running

Actual behaviour

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.html
import numpy as np
import cv2 as cv
# mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv.EVENT_LBUTTONDBLCLK:
        cv.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)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):
    cv.imshow('image',img)
    if cv.waitKey(20) & 0xFF == 27:
        break
cv.destroyAllWindows()
  • operating system: Ubuntu 18.04
  • architecture (e.g. x86): x64
  • opencv-python version: 4.3.0.36

Activity

  1. skvark commented on Jul 7, 2020

    @skvark
    Member

    The base manylinux image was changed between those versions. Qt version is the same 4.8.7.

  2. skvark commented on Jul 7, 2020

    @skvark
    Member

    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:

    import numpy as np
    import cv2
    
    window_name = "test"
    
    def draw_circle(event,x,y,flags,param):
        if event == 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 found
    while not window_open:
        try:
            cv2.namedWindow(window_name)
            cv2.setMouseCallback(window_name, draw_circle)
            window_open = True
        except:
            # destroy any "erroneous" windows OpenCV might have created
            cv2.destroyAllWindows()
        	
    while(1):
        cv2.imshow(window_name, img)
        if cv2.waitKey(20) & 0xFF == 27:
            break
            
    cv2.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.

  3. anqixu commented on Jul 7, 2020

    @anqixu
    Author

    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.

  4. skvark commented on Jul 7, 2020

    @skvark
    Member

    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.

  5. skvark commented on Jul 28, 2020

    @skvark
    Member

    Closing, please see #362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions