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


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



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
cv2.imshow('frame',frame)

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



When I use cv2.waitKey(1), I get a continuous live video feed from my laptops webcam, but 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?




2 Answers
2



From the doc - https://docs.opencv.org/2.4/modules/highgui/doc/user_interface.html?highlight=waitkey



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


waitKey(0)



2.waitKey(1) will display a frame for 1 ms, after which display will be automatically closed


waitKey(1)



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 1 ms only.


waitKey(0)


waitKey(1)





1 will wait for at least 1 ms. On Windows typocally like 10 ms afaik by default. Maybr waitKey will wait at least as long as rendering the images neefs, but not certain.
– Micka
Jul 2 at 22:21





@Micka Thanks for the comment. You may be right but I don't see that in the DOC.
– user8035311
Jul 2 at 22:24



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


cv2.waitKey(delay)


delay



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


cv2.waitKey(1)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?