如何从视频中选择两个帧? opencv c ++

时间:2014-03-12 12:49:21

标签: c++ opencv image-processing

我想从视频中选择两个帧。以下是我的代码,但它只能选择第一帧。有人能帮助我吗?感谢〜

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++;
}

2 个答案:

答案 0 :(得分:3)

这里的问题是

long currentFrame = frameOne;

应该是

long currentFrame = 1;

代码:

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++;
}

答案 1 :(得分:1)

如果您只是需要在不观看视频的情况下获取帧,您可以跳转到您正在寻找的帧:

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();

它适用于OpenCV 2.4.6,我不确定以前的版本。