本文介绍了OpenCV:读取视频序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种在OpenCV中随机读取视频帧的方法?就像使用索引访问数组一样?否则,如果我想在CPU或GPU中加载完整的视频,该怎么办?
Is there a way of randomly reading video frames in OpenCV....just like arrays are accessed using indexes? Otherwise, if I want to load complete video in CPU or GPU how can I do it?
推荐答案
您可以在视频捕获中使用set(int propId, double value)
方法(另请参阅文档),其中propId
可以是以下之一:
You can use the set(int propId, double value)
method on your video capture (also check out the documentation), where propId
can either be one of the following:
-
CV_CAP_PROP_POS_MSEC
:视频文件的当前位置,以毫秒为单位. -
CV_CAP_PROP_POS_FRAMES
:接下来要解码/捕获的帧的从0开始的索引. -
CV_CAP_PROP_POS_AVI_RATIO
:视频文件的相对位置:0-电影的开始,1-电影的结束.
CV_CAP_PROP_POS_MSEC
: Current position of the video file in milliseconds.CV_CAP_PROP_POS_FRAMES
: 0-based index of the frame to be decoded/captured next.CV_CAP_PROP_POS_AVI_RATIO
: Relative position of the video file: 0 - start of the film, 1 - end of the film.
一个播放视频50秒的小例子:
A small example that plays a video 50 seconds in:
int main( int argc, char **argv )
{
namedWindow("Frame", CV_WINDOW_NORMAL);
Mat frame;
VideoCapture capture(argv[1]);
if (!capture.isOpened())
{
//error in opening the video input
cerr << "Unable to open video file: " << argv[1] << endl;
exit(EXIT_FAILURE);
}
capture.set(CV_CAP_PROP_POS_MSEC, 50000);
for (;;)
{
//read the current frame
if (!capture.read(frame))
{
cerr << "Unable to read next frame." << endl;
cerr << "Exiting program!" << endl;
exit(EXIT_FAILURE);
}
imshow("Frame", frame);
waitKey(20);
}
}
这篇关于OpenCV:读取视频序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!