Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

opencv

vector

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;
}
like image 374
NewLearner Avatar asked Feb 14 '23 13:02

NewLearner


1 Answers

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;
}
like image 179
sgarizvi Avatar answered Feb 16 '23 02:02

sgarizvi