NewLearner
NewLearner

Reputation: 53

c++ How to put the video sequence into a vector<Mat> in OpenCV?

I am a new learner in c++. I read a video and I want to save the image sequence of the video into a vector called vector frame. The following is my code, please help me correct it if someone could, thank you a lot!

#include <cv.h>
#include <highgui.h>
#include <iostream>

#include <vector>

using namespace std;
using namespace cv;
int main()
{
VideoCapture capture("/home/P1030.MOV");

int totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);

vector<Mat> frame;
namedWindow("Display", WINDOW_AUTOSIZE);

for(int i=0; i < totalFrameNumber; i++)
{
    frame.push_back(Mat());
    imshow("Display", frame);
}
return 0;
}

Upvotes: 4

Views: 3191

Answers (1)

sgarizvi
sgarizvi

Reputation: 16816

You can do it as follows, although it is not recommended to load whole video in the memory at a single time.

int main()
{
    VideoCapture capture("/home/P1030.MOV");

    int totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT);

    Mat currentFrame;
    vector<Mat> frames;
    namedWindow("Display", WINDOW_AUTOSIZE);

    for(int i=0; i < totalFrameNumber; i++)
    {
       capture>>currentFrame;
       frames.push_back(currentFrame.clone());
       imshow("Display", currentFrame);
       waitKey(10);
    }
    return 0;
}

Upvotes: 8

Related Questions