Ask Your Question
0

cannot get two different frames from the video

asked 2014-01-22 02:43:37 -0600

cilem gravatar image

updated 2014-01-22 03:16:00 -0600

Hi to everyone. I am quite new on OpenCV. My goal is getting two frames from the video at any time wanted(like one from 10 msec and the other one is 1000 msec). I write this simple code below.

int main( int argc, char* argv[] )
{   
   Mat frame1,frame2;   
   VideoCapture capt("drop.avi");   
   capt.set(CV_CAP_PROP_POS_MSEC,10);   
   capt.read(frame1);   
   capt.set(CV_CAP_PROP_POS_MSEC,1000); 
   capt.read(frame2);         //if I comment out this line, frame1 is ok    
   namedWindow("w1",CV_WINDOW_AUTOSIZE);    
   namedWindow("w2",CV_WINDOW_AUTOSIZE);    
   imshow("w1",frame1); 
   imshow("w2",frame2);     
   waitKey(2000);   
   return 0;
}

Within this code, I get two exactly same frames that belongs to 1000 msec of the video. When I comment out "capt.read(frame2)" line, frame1 is ok(shows the frame at 10msec). I also try set frames but the result is same,unfortunately. By the way, I am using OpenCV 245 on VS2010.Is there anything I miss? Any helps would be appreciated.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-01-22 03:43:03 -0600

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;
}
edit flag offensive delete link more

Comments

1

Thank you for your excellent reply. Since I stick to definitions, I could not see this. Great, again.

cilem gravatar imagecilem ( 2014-01-22 05:53:29 -0600 )edit

You are welcome. Please accept this as a correct answer if it satisfies your needs.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-01-22 06:05:17 -0600 )edit

Question Tools

Stats

Asked: 2014-01-22 02:43:37 -0600

Seen: 1,214 times

Last updated: Jan 22 '14