繁体   English   中英

如何从视频中选择两个帧? OpenCV的C + +

[英]How to select two frames from a video? opencv c++

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

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

这里的问题是

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

如果您只需要在不观看视频的情况下获得框架,则可以跳至要查找的框架:

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,我不确定以前的版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM