Arthur Arthur - 1 year ago 272
Python Question

DestroyWindow does not close window on Mac using Python and OpenCV

My program generates a series of windows using the following code:

def display(img, name, fun):
global clicked

cv.NamedWindow(name, 1)
cv.ShowImage(name, img)
cv.SetMouseCallback(name, fun, img)

while cv.WaitKey(33) == -1:
if clicked == 1:
clicked = 0
cv.ShowImage(name, img)


I press "q" within the gui window to close it. However, the code continues to the next call of the display function and displays a second gui window while not closing the first. I'm using a Mac with OpenCV 2.1, running the program in Terminal. How can I close the gui windows? Thanks.

Answer Source

There are a few peculiarities with the GUI in OpenCV. The DestroyImage call fails to close a window (atleast under Linux, where the default backend was Gtk+ until 2.1.0) unless WaitKey was called to pump the events. Adding a WaitKey(1) call right after DestroyWindow may work.

Even so, closing is not guaranteed; the the WaitKey function is only intercepted if a window has focus, and so if the window didn't have focus at the time you invoked DestroyWindow, chances are it'll stay visible till the next DestroyWindow call.

I'm assuming this is a behaviour that stems from Gtk+; the function didn't give me any trouble when I used it under Windows.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download