Search code examples
pythonopencvosx-mavericks

Trouble with plots in opencv 2.4.7, python 2.7.6, OS X Mavericks


I've installed python, opencv, and a whole slew of dependancies using brew. For the most part, everything works. Where I run into problems is trying to display and interact with an image.

If I do:

import cv2
im=cv2.imread('myimage.jpg')
cv2.imshow('main',im)

Then the image displays, with no way to interact with it at all, because the window is marked as unresponsive by the operating system (ie, if I mouse over the window, I get the dreaded beachball). I can still close the window using cv2.destroyWindow('main').

My big problem is trying to display an image that is too large to fit on the screen. Doing some reading here, I saw that I needed to install qt, and then build opencv using the qt backend. So I brew uninstalled opencv, I brew installed qt, then brew installed opencv with the --with-qt build option.

Now when I try doing that test (or try using cv2.namedWindow, which gives me the same result) I get a window that has some buttons at the top (some directional arrows, a disk icon, etc) and a black empty content window. No image is ever displayed. And I still have the beachball. This leads me to believe there's a problem with the unresponsiveness...the plot buttons are useless without interactivity, so the non-interactivity of the image window can't be by design.


Solution

  • Testing the first example, i got a windows that disappeared instantly. So your program might have crashed when the window was supposed to be removed. When the program reaches the end of execution everything is cleaned up and removed after all.

    This fixed it for me, can't test the qt version since i don't have opencv with qt installed.

    cv2.waithkey(5) waits 5 ms, and then returns the key code of any key pressed. So it won't get past the while loop untill escape is pressed.

    import cv2
    
    im=cv2.imread('test.jpg')
    cv2.imshow('main',im)
    
    #find ascii key codes here  http://www.asciitable.com/
    #Escape == 27
    while (cv2.waitKey(5) != 27): pass