Difference in output with waitKey(0) and waitKey(1)


Python Problem Overview

I've just begun using the OpenCV library for Python and came across something I didn't understand.

cap = cv2.VideoCapture(0)

while True:
      ret, frame = cap.read() #returns ret and the frame

      if cv2.waitKey(1) & 0xFF == ord('q'):

When I use cv2.waitKey(1), I get a continuous live video feed from my laptops webcam. However when I use cv2.waitKey(0), I get still images. Every time I close the window, another one pops up with another picture taken at the time. Why does it not show as a continuous feed?

Python Solutions

Solution 1 - Python

From the doc:

1.waitKey(0) will display the window infinitely until any keypress (it is suitable for image display).

2.waitKey(1) will display a frame for 1 ms, after which display will be automatically closed. Since the OS has a minimum time between switching threads, the function will not wait exactly 1 ms, it will wait at least 1 ms, depending on what else is running on your computer at that time.

So, if you use waitKey(0) you see a still image until you actually press something while for waitKey(1) the function will show a frame for at least 1 ms only.

Solution 2 - Python

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(1) will wait for keyPress for just 1 millisecond and it will continue to refresh and read frame from your webcam using cap.read().

More clearly, Use debugger in your code.When using waitKey(0) in the while loop, the debugger never crosses this statement and does not refresh the frame and hence the frame output seems stable.Does not move. Where as with waitKey(1), the debugger will cross the code after pausing at

if cv2.waitKey(1) & 0xFF == ord('q')

for 1 milli second.

Solution 3 - Python

From the documentation you can see that cv2.waitKey(delay) waits for delay milliseconds if delay is positive but forever (waits for a key event infinitely) if it's zero or negative. That's why you see these differences in behavior.

In the case of cv2.waitKey(1) this is, in fact, negligible but its use provides the user the opportunity to press a key (the key might be caught in some next iteration but does not make a big difference).

Solution 4 - Python

Delay in milliseconds. 0 is the special value that means “forever”. The function waitKey waits for a key event infinitely (when \texttt{delay}\leq 0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.

Note: 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. Note: The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active

Solution 5 - Python

cv2.waitkey(1) is being used in while loop.It shows the output for 1msec but because of infinite while loop it is the sequence of images that are perceived by our brain as a single continuos video.

Hope this helped.

Solution 6 - Python

Indratej Reddy's answer is the only one to clearly explain why this works — and why no GUI events act until waitkey is called. Unclear how to handle menu/mouse events — e.g., ignores menu quit.

$ python3
>>> import cv2
>>> im = cv2.imread("/tmp/portrait.jpg")
>>> print("im =", im)
im = […]
>>> cv2.imshow("image", im)
>>> print("imshow called")
imshow called
>>> print("waitKey =", cv2.waitKey(0))
waitKey = 32
>>> print("destroyAllWindows =", cv2.destroyAllWindows())
destroyAllWindows = None
>>> print("Final waitKey 1ms =", cv2.waitKey(1))
Final waitKey 1ms = -1


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOmar Pervez KhanView Question on Stackoverflow
Solution 1 - PythonAnatoliiView Answer on Stackoverflow
Solution 2 - PythonDikshit KathuriaView Answer on Stackoverflow
Solution 3 - PythonEyprosView Answer on Stackoverflow
Solution 4 - PythonIndratej ReddyView Answer on Stackoverflow
Solution 5 - PythonSabit AutiView Answer on Stackoverflow
Solution 6 - PythonDevonView Answer on Stackoverflow