简体   繁体   English

为什么没有cv2.waitkey()时cv2.imshow()无法渲染?

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

Without the cv2.waitkey() method the cv2.imshow() displays black window. 没有cv2.waitkey()方法,cv2.imshow()将显示黑色窗口。 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

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() . cv2.imshow()的文档中,“注释”部分提到该窗口显示的时间为cv2.waitKey()的参数所指示的时间。 An argument of 0 indicates wait forever, so the image will be displayed forever unless you handle the keypress. 参数0表示永远等待,因此除非您处理按键,否则图像将永远显示。

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.' cv2.waitKey()的文档中,“注释”部分提到“此函数是HighGUI中唯一可以获取和处理事件的方法,因此,除非在具有以下功能的环境中使用HighGUI,否则需要定期调用此函数以进行常规事件处理负责事件处理。”

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.waitKey() ,则将鼠标悬停在显示的窗口上时,将显示带有滚轮的“忙”光标,表明该窗口正忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM