tierratheseeress
tierratheseeress

Reputation: 37

Array of Mats from video file - opencv

I'm coding using C++ and opencv on linux. I've found this similar question; although, I can't quite get it to work.

What I want to do is read in a video file and store a certain number of frames in an array. Over that number, I want to delete the first frame and add the most recent frame to the end of the array.

Here's my code so far.

VideoCapture cap("Video.mp4");
int width = 2;
int height = 2;
Rect roi = Rect(100, 100, width, height);

 vector<Mat> matArray;
int numberFrames = 6;
int currentFrameNumber = 0;

for (;;){

    cap >> cameraInput;
    cameraInput(roi).copyTo(finalOutputImage);

    if(currentFrameNumber < numberFrames){
        matArray.push_back(finalOutputImage);
    }else if(currentFrameNumber <= numberFrames){
        for(int i=0;i<matArray.size()-1; i++){
            swap(matArray[i], matArray[i+1]);
        }
        matArray.pop_back();
        matArray.push_back(finalOutputImage);
    }

    currentFrameNumber++;
 }

My understanding of mats says this is probably a problem with pointers; I'm just not sure how to fix it. When I look at the array of mats, every element is the same frame. Thank you.

Upvotes: 1

Views: 1456

Answers (1)

a-Jays
a-Jays

Reputation: 1212

There's no need for all this complication if you were to make use of C++'s highly useful STL.

if( currentFrameNumber >= numberFrames )
    matArray.remove( matArray.begin() );
matArray.push_back( finalOutputImage.clone() );     //check out @berak's comment

should do it.

Upvotes: 3

Related Questions