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

2

7

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?

Suffumigate answered 21/7, 2012 at 14:35 Comment(2)
Did you find a solution for this or opened a bug? I am seriously tired of dealing with OpenCV issues on Linux.Hyperbaton
See this question: stackoverflow.com/questions/6116564Montague
W
2

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.

Whist answered 23/7, 2012 at 15:36 Comment(1)
Thanks for your advice. But whatever I change WaitKey() to WaitKey(0) or WaitKey(1), the window remained the same. The same code runs well under Windows 7. I wonder whether OpenCV has its unique way to create and close a window under Ubuntu? Or OpenCV does not support the newest Ubuntu system 12.04 LTS?Suffumigate
A
2

Try just this:

c = cv.WaitKey(27)
if c == 27:
   cv.DestroyAllWindows("Test")
   break
Admeasure answered 18/9, 2012 at 22:49 Comment(1)
Can you explain what this will do? From the docs I understand this will wait for a keypress for 27 milliseconds, then return -1 (or the keycode). Why compare c to 27 instead of a more descriptive constant name for that keycode?Discussant

© 2022 - 2024 — McMap. All rights reserved.