Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV DestroyWindow doesn't work on Ubuntu. How to close a window correctly?

In the following code, DestroyWindow or DestroyAllWindows can't close the window opened by ShowImage. When I tried to close it by clicking the close button, the window suspended. After killing the window, the whole IDLE closed.

import cv
image = cv.LoadImage("../lena.bmp", 0)
cv.NamedWindow("test")
cv.ShowImage("test", image)
cv.WaitKey()
cv.DestroyWindow("test")  #or cv.DestroyAllWindows()

I'm using OpenCV 2.4.2 with Python 2.7 on Ubuntu 12.04 LTS.

Am I did something wrong and how can i close the window create by ShowImage?

like image 407
123hurray Avatar asked Jul 21 '12 14:07

123hurray


People also ask

How do I close a window in OpenCV?

There are two documented methods to do this. First one is cvWaitKey() (just call cvWaitKey(1) after you close the last image). Second one is to call cvStartWindowThread() at the start of your program to allow OpenCV updating its windows automatically.

How do I close an image after cv2 Imshow?

destroyAllWindows(). Inside the cv2. waitKey() function, you can provide any value to close the image and continue with further lines of code.

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.


1 Answers

I believe cv.WaitKey should be called with a number as an argument, either 0 or n > 0, where n>0 specifies the number of milliseconds to wait.

cv.WaitKey(0) will wait indefinitely for a keyboard press, and then return the character input. Pressing a keyboard button should close the window, if you haven't tried that already.

like image 109
hang Avatar answered Sep 30 '22 04:09

hang