简体   繁体   English

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

[英]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.

相关问题 无法使用带有 OpenCV 的 C++ 从视频中提取帧 - Can't extract frames from video using C++ with OpenCV 如何使用 OpenCV 而不是 C++ 将视频分成帧 - How to break a video into frames using OpenCV over C++ 如何将帧从 OpenCV C++ 代码流式传输到 Video4Linux 或 ffmpeg? - How to stream frames from OpenCV C++ code to Video4Linux or ffmpeg? OpenCV / C ++-从视频文件读取的帧在视频结束后重新启动 - OpenCV/C++ - frames read from video file restart after end of the video 无论如何,我可以使用数组在opencv c ++中存储所有视频帧(无论视频多长时间)吗? - Is there anyway I can use array to store all video frames (no matter how long the video is) in opencv c++? 如何使用OpenCV(c ++)存储序列中的所有帧? - How to store all frames from a sequence with OpenCV (c++)? 调试一种使用C ++中的OpenCV从视频中查找帧的移动平均值以进行运动检测的方法 - debugging a method for finding running average of frames from video for motion detection using OpenCV in C++ OpenCV VideoCapture称视频有0帧(C ++和Python) - OpenCV VideoCapture says video has 0 frames (C++ and Python) 如何使用OpenCV4 C ++从c920摄像机获取帧 - How to get frames from c920 camera using OpenCV4 C++ 在opencv C++中从向量中检索固定帧数 - Retrieve fix number of frames from vector in opencv c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM