Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)      cv.DestroyWindow(name) 

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.

like image 929
Arthur Avatar asked May 24 '11 20:05

Arthur


People also ask

How do I close windows on OpenCV?

Python OpenCV – destroyAllWindows() Function Python Opencv destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. If you have multiple windows open at the same time and you want to close then you would use this function.

What does cv2 destroyAllWindows () do?

0 – wait indefinitely cv2. destroyAllWindows() simply destroys all the windows we created. To destroy any specific window, use the function cv2. destroyWindow() where you pass the exact window name.

What is cv2 namedWindow?

Python OpenCV namedWindow() method is used to create a window with a suitable name and size to display images and videos on the screen. The image by default is displayed in its original size, so we may need to resize the image for it to fit our screen.

What is cv2 Waitkey?

cv2 waitkey() allows you to wait for a specific time in milliseconds until you press any button on the keyword. It accepts time in milliseconds as an argument.


2 Answers

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.

like image 135
susmits Avatar answered Sep 20 '22 13:09

susmits


You need to run cv.startWindowThread() after opening the window. I had the same issue and now this works for me.

Hope this helps for future readers. And there is also a cv2 binding (I advise to use that instead of cv).

This code works for me:

import cv2 as cv import time  WINDOW_NAME = "win"  image = cv.imread("ela.jpg", cv.CV_LOAD_IMAGE_COLOR) cv.namedWindow(WINDOW_NAME, cv.CV_WINDOW_AUTOSIZE) initialtime = time.time()  cv.startWindowThread()  while (time.time() - initialtime < 5):   print "in first while" cv.imshow(WINDOW_NAME, image) cv.waitKey(1000)  cv.waitKey(1) cv.destroyAllWindows() cv.waitKey(1)  initialtime = time.time() while (time.time() - initialtime < 6):     print "in second while" 

The same issue happens with the C++ version, on Linux: Trying to close OpenCV window has no effect

like image 38
elaRosca Avatar answered Sep 19 '22 13:09

elaRosca