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?

123hurray
  • 71
  • 1
  • 4

2 Answers2

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.

hang
  • 51
  • 1
  • 2
    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? – 123hurray Jul 24 '12 at 01:32
2

Try just this:

c = cv.WaitKey(27)
if c == 27:
   cv.DestroyAllWindows("Test")
   break
Anne
  • 26,765
  • 9
  • 65
  • 71
  • 2
    Can you explain what this will do? From the [docs](http://opencv.willowgarage.com/documentation/python/user_interface.html#waitkey) 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? – Chris Wesseling Sep 19 '12 at 16:29