Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the cv2.imshow() does not render without cv2.waitkey()?

Without the cv2.waitkey() method the cv2.imshow() displays black window. Why does the rendering does not happen properly without the wait?

cap = cv2.VideoCapture(video_path)
while cap.isOpened():
    ret,frame = cap.read()
    cv2.imshow('window-name',frame)
    # without the following cv2.waitkey(1) statement the cv2.imshow() displays a blank window
    if cv2.waitKey(1) & 0xFF == ord('q'): # wait for 1 millisecond
        break
    continue
like image 994
Smith453 Avatar asked May 17 '26 17:05

Smith453


1 Answers

From the documentation of cv2.imshow(), the NOTE section mentions that the window is displayed for the amount of time indicated by the argument in cv2.waitKey(). An argument of 0 indicates wait forever, so the image will be displayed forever unless you handle the keypress.

Controlling the duration for which the window needs to displayed is a useful aspect while debugging, displaying intermediate images, etc.

From the documentation of cv2.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.waitKey(), if you hover over the window that is displayed, the 'busy' cursor with the rolling wheel is displayed, indicating that the window is busy.

like image 148
Shawn Mathew Avatar answered May 24 '26 17:05

Shawn Mathew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!