Ask Your Question

Revision history [back]

Actually your problem is quite logical. The capture.read() function creates a pointer to the current capture object frame and assigns that internal pointer of the capture object to the frame at 10 ms. When you switch that pointer to the frame at 1000 ms and hang it to frame2 matrix, actually both matrices are having an internal pointer pointing to the same problem.

To solve this problem you need to make a hard copy as seen in the following code snippet. Here the variables are stored in two new hard copies called temp1 and temp2. I also added a frame called difference, to see that you have actually two different frames!

#include "opencv2/opencv.hpp";

using namespace std;
using namespace cv;

int main( int argc, char* argv[] )
{   
   Mat frame1, frame2;
   Mat temp1, temp2;
   VideoCapture capture("drop.avi");   

   capture.set(CV_CAP_PROP_POS_MSEC,10);   
   capture >> frame1;
   temp1 = frame1.clone();

   capture.set(CV_CAP_PROP_POS_MSEC,1000); 
   capture >> frame2;
   temp2 = frame2.clone();

   imshow("w1", temp1); 
   imshow("w2", temp2);   

   Mat difference = temp1 - temp2;
   imshow("difference", difference);

   waitKey(0); 

   return 0;
}