Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从视频中选择两帧?OpenCV C++_C++_Opencv_Image Processing - Fatal编程技术网

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

如何从视频中选择两帧?OpenCV C++,c++,opencv,image-processing,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;

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

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 currentFrame = frameOne;
应该是

long currentFrame = 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,我不确定是否适用于以前的版本

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

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,我不确定是否适用于以前的版本

对不起,我试过你的方法了。但结果是一样的。因此,我认为问题是由if条件引起的。无论如何,谢谢你!对不起,我试过你的方法了。但结果是一样的。因此,我认为问题是由if条件引起的。无论如何,谢谢你!哇,太棒了!我用的是OpenCV2.4.3,现在可以用了。非常感谢你!我很高兴它对你有用。你能接受答案吗?很抱歉,我刚才看到你的留言,我接受了!非常感谢。哇,太棒了!我用的是OpenCV2.4.3,现在可以用了。非常感谢你!我很高兴它对你有用。你能接受答案吗?很抱歉,我刚才看到你的留言,我接受了!非常感谢。