1

I just started to learn OpenCV with Python for Raspberry Pi 3. I wrote a simple code for namedWindow(). The problem is destroyWindow() does not work as I expect. The "TEST" window is still there. What is wrong with the following code?

import cv2
import sys

if __name__ == '__main__':
    cv2.namedWindow("TEST")
    while True:
        key = cv2.waitKey(5)
        if key == 27:
            print "ESC pressed..."
            cv2.destroyWindow("TEST")
            break
    sys.exit()
elhefe
  • 3,404
  • 3
  • 31
  • 45
Y. Kim
  • 11
  • 2
  • You may want to check out [this link](http://stackoverflow.com/questions/6116564/destroywindow-does-not-close-window-on-mac-using-python-and-opencv). – Aenimated1 Apr 09 '16 at 04:02
  • move the destroyWindow("TEST") out of the loop. – kcc__ Apr 09 '16 at 05:30
  • When I was last working with `destroyWindow`, I had to not only put a few different calls of `waitKey` before and after the `destroyWindow` (two in front, five after), I also had to start a `cvWindowThread` right before I created the `namedWindow`. Then I called `std::terminate()` at then end, as the thread wasn't closing properly (mind, that was in C++, python might handle the threads better). – Rachel L Apr 13 '16 at 17:41

1 Answers1

0

Here's what usually works for me as far as the waitKey goes:

if cv2.waitKey(1) & 0xFF == 27:
    break
cv2.destroyWindow("TEST")

Which is a bitwise and with the waitkey input and 11111111 which should equal the UTF-8 value for the key. I tested this on This Loop. Hope this helps.

Community
  • 1
  • 1
ThisGuyCantEven
  • 1,095
  • 12
  • 21