Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OpenCV - waitKey(0) does not respond?

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?

like image 478
Dhorka Avatar asked Dec 12 '13 09:12

Dhorka


People also ask

What happens if we pass 0 to waitKey ()?

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.

How do I exit cv2 waitKey 0?

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.

What does cv2 waitKey 0 mean?

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.

Is cv2 waitKey necessary?

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.


2 Answers

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

like image 52
user2022581 Avatar answered Oct 18 '22 21:10

user2022581


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) 
like image 33
Teng Long Avatar answered Oct 18 '22 20:10

Teng Long