title: Array of Mats from video file - opencv
body: <p>I'm coding using C++ and opencv on linux. I've found <a href="https://stackoverflow.com/questions/9138537/opencv-creating-an-array-of-mat-objects">this</a> similar question; although, I can't quite get it to work.</p>
  
  <p>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.</p>
  
  <p>Here's my code so far.</p>
  
  <pre><code>VideoCapture cap("Video.mp4");
  int width = 2;
  int height = 2;
  Rect roi = Rect(100, 100, width, height);
  
   vector&lt;Mat&gt; matArray;
  int numberFrames = 6;
  int currentFrameNumber = 0;
  
  for (;;){
  
      cap &gt;&gt; cameraInput;
      cameraInput(roi).copyTo(finalOutputImage);
  
      if(currentFrameNumber &lt; numberFrames){
          matArray.push_back(finalOutputImage);
      }else if(currentFrameNumber &lt;= numberFrames){
          for(int i=0;i&lt;matArray.size()-1; i++){
              swap(matArray[i], matArray[i+1]);
          }
          matArray.pop_back();
          matArray.push_back(finalOutputImage);
      }
  
      currentFrameNumber++;
   }
  </code></pre>
  
  <p>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.</p>