问题描述
import cv
# create a window
winname = "myWindow"
win = cv.NamedWindow(winname, cv.CV_WINDOW_AUTOSIZE)
# load video file
invideo = cv.CaptureFromFile("video.avi")
# interval between frame in ms.
fps = cv.GetCaptureProperty(invid, cv.CV_CAP_PROP_FPS)
interval = int(1000.0 / fps)
# play video
while (True):
im = cv.QueryFrame(invideo)
cv.ShowImage(winname, im)
if cv.WaitKey(interval) == 27: # ASCII 27 is the ESC key
break
del invideo
cv.DestroyWindow(winname)
上面是使用opencv libraray播放视频文件的简单python代码.
Above is a simple python code using opencv libraray to play a video file.
我唯一不了解的部分是im = cv.QueryFrame(invideo)
The only part I don't understand is im = cv.QueryFrame(invideo)
根据 opencv api ,"QueryFrame
从摄像机或视频文件中抓取一帧,将其解压缩并返回."
According to opencv api, " QueryFrame
grabs a frame from a camera or video file, decompresses it and returns it."
据我了解,它只是以iplimage
格式返回一帧图像,但是如何知道返回的是哪一帧?唯一需要参数QueryFrame
的是视频捕获,但是没有索引可以告诉它我需要检索的视频帧的数量.如果我需要从中间开始播放视频怎么办?
For my understanding, it just returns an image in iplimage
format for one single frame, but how does it know which frame it returns? The only parameter QueryFrame
need is the video capture, but there no index to tell it which frame amount the video frames I need to retrieve. What if I need to play a video starting from middle part?
推荐答案
您必须将cv.GetCaptureProperty
与CV_CAP_PROP_FRAME_COUNT一起使用才能获取视频的帧数.将其除以2可找到中间值.使用QueryFrame
直到达到该值.
You have to use cv.GetCaptureProperty
with CV_CAP_PROP_FRAME_COUNT to get the number of frames of your video.Divide it by 2 to find the middle.Use QueryFrame
until you reach this value.
这篇关于QueryFrame如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!