繁体   English   中英

用户提示下逐帧进行视频处理

[英]Video Processing frame-by-frame at user prompt

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

有没有有效的方法可以在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()

您可以使用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

暂无
暂无

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

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