I'm using opencv 2.4.7 on ubuntu 12.04. I'm programming with python and I have a problem when i run this script:
import cv2 img = cv2.imread('347620923614738322_233985812.jpg') cv2.namedWindow("window") cv2.imshow("window", img) cv2.waitKey(0)
The problem is that the script doesn't stop when I close the image. I searched information about waitKey
and I found that using cv2.waitKey(0)
is correct.
I don't understand, where is the problem?
waitKey(0) will pause your screen because it will wait infinitely for keyPress on your keyboard and will not refresh the frame( cap. read() ) using your WebCam.
waitKey(0) until the 0 key is pressed properly whilst the window is in focus. Pressing the 0 key whilst the window is in focus is the right way to close the window and make sure that, once the window is destroyed in line cv2. destroyAllWindows() and the program ends, the user can get back the control of the terminal.
Python OpenCV – waitKey() Function waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed.
waitKey() , the NOTE section mentions that 'This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing. ' You can notice that without the cv2.
I found that it works if i press the key whilst the window is in focus. If the command line is in focus then nothing happens
Adding a cv2.waitKey(1) after you destroy the window should work in this case.
cv2.imshow('imgae',img) cv2.waitKey(0) cv2.destroyAllWindows() cv2.waitKey(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With