【问题标题】:Video Processing frame-by-frame at user prompt在用户提示下逐帧处理视频
【发布时间】:2017-03-29 20:48:42
【问题描述】:

我希望处理一些视频数据,并希望逐帧处理。我希望看到每一帧的结果,所以希望在帧处理之间有暂停,被用户输入(可能是一个键)打破。

在 Python 中是否有有效的方法来实现这一目标? 这是我当前的代码,目前它继续运行,我不确定这是否是最好的方法?

**编辑:** 我已更改代码以实现我的目标。只需要添加另一行 waitKey 命令。 :p

import cv2
import scipy.ndimage as ndimage

cap = cv2.VideoCapture("#Some_Video")

while(True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    fil_im = ndimage.correlate(gray, '#Some_filter')
    cv2.imshow('filtered figure', fil_im)

    k = cv2.waitKey(0)
    if k == 27:
            break
    elif k == 32:
            continue
    # Continues to the next frame on 'space', quits the loop on 'esc' key.

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python python-2.7 opencv video-processing


    【解决方案1】:

    您可以使用cv2.waitKey(0) 来暂停while 循环的迭代。参数 0 表示代码将无限期地等待您按下某个键,然后再移动到下一行代码。

    修改代码为:

    while(True):
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        fil_im = ndimage.correlate(gray, '#Some_filter')
        cv2.imshow('filtered figure', fil_im)
        cv2.waitKey(0)# waits for user prompt here 
    
        if cv2.waitKey(0) == 'b':
                break
                #breaks while loop only if you press b, else it goes to next iteration
    

    【讨论】:

    • 谢谢!我确实使用了两个 waitKey 命令。一个用于继续下一帧,一个用于完全停止循环。
    • 我希望能回答你的问题,考虑到反对意见
    • 不知道你为什么投了反对票。 :S 但至少我投了一票。
    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2011-03-29
    相关资源
    最近更新 更多