python快速检测视频跳过帧,在Python / OpenCV中有一种快速滚动视频帧的方法,允许用户选择要处理的开始和结束帧吗?...

In preparing to process a video I want the user to be able to select the first and last frame to be processed in the video. The trackbar seems like a useful tool to do this but can I use it to read and display specific frames from a video?

Typically I read a video in frame-by-frame and run my processing algorithm on it, using a while loop:

cap = cv2.VideoCapture('myvideo.mp4')

while(cap.isOpened()):

ret, frame = cap.read()

# ....

This is not conducive to having the user quickly scan through the video to find a good frame interval to process.

The trackbar is great for setting image processing parameters, but if there is a better tool that you can think of for this please suggest. Below you can see some code for setting a threshold level variable using a trackbar.

def onTrackbarChange(trackbarValue):

pass

cv2.createTrackbar( 'threshold level', 'mywindow', 100, 255, onTrackbarChange )

thresholdlevel = cv2.getTrackbarPos('thresh','mywindow')

Is there a way to do something like this?

start_frame = cv2.getTrackbarPos('start-frame','mywindow')

ret, frame = cap.read(start_frame) #don't think this is possible

cv2.imshow('window', frame)

Ideally there would be two window panels, one with the start_frame, and one with the stop_frame, each controlled by a trackbar.

解决方案

first of all, you can set the video position with:

cap.set(cv2.CAP_PROP_POS_FRAMES,pos)

then it's just putting together the pieces, let them play with the trackbars, when a key was pressed, play the interval:

import cv2

cap = cv2.VideoCapture('david.mpg')

length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

def onChange(trackbarValue):

cap.set(cv2.CAP_PROP_POS_FRAMES,trackbarValue)

err,img = cap.read()

cv2.imshow("mywindow", img)

pass

cv2.namedWindow('mywindow')

cv2.createTrackbar( 'start', 'mywindow', 0, length, onChange )

cv2.createTrackbar( 'end' , 'mywindow', 100, length, onChange )

onChange(0)

cv2.waitKey()

start = cv2.getTrackbarPos('start','mywindow')

end = cv2.getTrackbarPos('end','mywindow')

if start >= end:

raise Exception("start must be less than end")

cap.set(cv2.CAP_PROP_POS_FRAMES,start)

while cap.isOpened():

err,img = cap.read()

if cap.get(cv2.CAP_PROP_POS_FRAMES) >= end:

break

cv2.imshow("mywindow", img)

k = cv2.waitKey(10) & 0xff

if k==27:

break

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值