[英]How to select two frames from a video? opencv c++
I want to select two frames from a video. 我想从视频中选择两个帧。 The following is my code, but it can only select the first frame.
以下是我的代码,但只能选择第一帧。 Could someone help me?
有人可以帮我吗? Thanks~
谢谢〜
long frameOne = 2130;
long frameTwo = 2160;
long currentFrame = frameOne;
bool stop = false;
while(!stop)
{
if(!capture.read(frame))
{
cout << "Failed to read the video!" << endl;
return -1;
}
// select the first image
if(currentFrame == frameOne)
{
frame1 = frame;
}
if(currentFrame == frameTwo)
{
frame2 = frame;
}
if(currentFrame>frameTwo)
{
stop = true;
}
currentFrame++;
}
The problem here is that 这里的问题是
long currentFrame = frameOne;
should be 应该
long currentFrame = 1;
Code: 码:
long frameOne = 2130;
long frameTwo = 2160;
long currentFrame = 1;
bool stop = false;
while(!stop)
{
if(!capture.read(frame))
{
cout << "Failed to read the video!" << endl;
return -1;
}
// select the first image
if(currentFrame == frameOne)
{
// it needs a deep copy, since frame points to the static capture mem
frame1 = frame.clone();
}
if(currentFrame == frameTwo)
{
frame2 = frame.clone();
}
if(currentFrame>frameTwo)
{
stop = true;
}
currentFrame++;
}
If you just need to get the frames without watching the video you can just jump to the frame you are looking for with: 如果您只需要在不观看视频的情况下获得框架,则可以跳至要查找的框架:
cv::Mat frame,frame1,frame2;
capture = cv::VideoCapture(videoFname);
if( !capture.isOpened( ) )
{
std::cout<<"Cannot open video file";
exit( -1 );
}
capture.set(CV_CAP_PROP_POS_FRAMES,frameOne);
capture >> frame;
frame1 = frame.clone();
capture.set(CV_CAP_PROP_POS_FRAMES,frameTwo);
capture >> frame;
frame2 = frame.clone();
It works with OpenCV 2.4.6, I am not sure for previous versions. 它适用于OpenCV 2.4.6,我不确定以前的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.