python - 如何让 cv2.imshow() 显示列表中的帧?

标签 python opencv

我正在尝试将网络摄像头的最后 50 帧保存到列表中,然后播放这些帧。当我尝试显示框架时,显示窗口显示灰色并表示它没有响应。如果我在 while 循环中显示帧,它会显示,但如果我尝试显示列表中的帧,我将它们保存在上述问题中。这是我正在运行的代码。

cap = cv2.VideoCapture(0)
image_list = []
count = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    image_list.append(frame)

    #Display the resulting frame
    #cv2.imshow('frame',frame)   <---  this will show me my live frame by frame capture

    if count >= 50:
        break

    count += 1

# When everything is done, release the capture
cap.release()

for image in image_list:  
    cv2.imshow("frame", image)
    sleep(1)

最佳答案

如果你没有使用像 tkinter 或 Qt 这样的正确的 UI Frameworkrwork,你必须调用

cv2.waitKey(500)

定期,因为这是 OpenCv 的 Highgui 组件处理事件(并更新显示)的唯一方法。否则 highgui 只是“挂起”。

for image in image_list:  
    cv2.imshow("frame", image)
    cv2.waitKey(500)

摘自docs :

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.

关于python - 如何让 cv2.imshow() 显示列表中的帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59889892/

相关文章:

c++ - opencv标量中的错误值

python-3.x - Python 3D 图像分割在分水岭的距离图中找到局部峰值

python - 元组数字到数字的转换

python - 如何在kivy中将属性从一个类传递到另一个类

python - 如何根据字符拆分数据框列并保留该字符?

python - TfIdfVectorizer 未正确标记

python - 我正在寻找 python 中 FDIST 的等效函数

python-3.x - cv2。错误:C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:325:错误:(-215)size.width> 0 && size.height> 0在函数cv::imshow中

python - 添加多个重叠立方体的矢量化方式

OpenCV : Building a simple 3d model