为什么 cv2.imshow() 在没有 cv2.waitkey() 的情况下不呈现? [英] Why does the cv2.imshow() does not render without cv2.waitkey()?

查看:175
本文介绍了为什么 cv2.imshow() 在没有 cv2.waitkey() 的情况下不呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有 cv2.waitkey() 方法,cv2.imshow() 会显示黑色窗口.为什么没有等待就不能正常渲染?

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

推荐答案

来自 cv2.imshow(),NOTE 部分提到窗口显示的时间由 cv2.waitKey() 中的参数指示.0 的参数表示永远等待,因此除非您处理按键,否则图像将永远显示.

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.

来自 cv2.waitKey() 的文档,NOTE 部分提到'该函数是 HighGUI 中唯一可以获取和处理事件的方法,因此需要定期调用它以进行正常的事件处理,除非在需要关注事件处理.'

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.'

你可以注意到,如果没有cv2.waitKey(),如果你将鼠标悬停在显示的窗口上,会显示带有滚轮的'忙'光标,表示该窗口很忙.

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.

这篇关于为什么 cv2.imshow() 在没有 cv2.waitkey() 的情况下不呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆