Faster frame capture from Opencv webcam

I am developing a real-time optical flow application with Opencv (C ++). I donโ€™t understand how to capture two consecutive frames to apply the Lucas-Kanade tracking method.

This does not work:

CvCapture* capture = cvCaptureFromCAM(1); IplImage *imgA=cvQueryFrame( capture ); IplImage *imgB=cvQueryFrame( capture ); 

I also tried this, but the program does not exit the loop:

 CvCapture* capture = cvCaptureFromCAM(1); IplImage *imgA=cvQueryFrame( capture ); IplImage *imgB=cvCreateImage(cvSize(imgA),IPL_DEPTH_32F,3); while(cvNorm(imgA,imgB)==0) imgB=cvQueryFrame( capture ); 

Any ideas? I hope this is not a stupid question, but I suspect this is: / Sorry in advance. Thanks!!

+4
source share
4 answers
 cv::Mat m1, m2; cv::VideoCapture cap(0); if(!cap.isOpened()) ;// ... throw error here cap >> m1; cap >> m2; // m1 and m2 now contain consecutive frames. 
+3
source

I will explain why the source code does not work. cvQueryFrame reuses the same buffer. Therefore, when you do:

 IplImage *imgA=cvQueryFrame( capture ); 

you get a pointer to your internal buffer where the image is stored. Now that you do:

 IplImage *imgB=cvQueryFrame( capture ); 

the image is overwritten and a pointer to the same buffer is indicated. IMGA == ImgB. You must make a copy after requesting the first frame, then everything will work.

+1
source

one query before the loop, and another inside the loop may be enough here (Pseudocode):

 IplImage prev = query(capture) while(1) next = query(capture ) opticalflow( prev, next ) prev = cvCopy( next ) 
+1
source
 Mat* mImg; IplImage* _image; IplImage* image; CvCapture* capture = cvCaptureFromCAM(0); for (int i = 0; i < 15; i++) { _image = cvQueryFrame(capture); } *image = *_image; 

I always meet the situation 0xC0000005 with this. Interestingly, something is wrong in my code. I use image = _image because I think _image = cvQueryFrame(capture); just points to the capture buffer, so I can save the frame in a different memory situation.

0
source

All Articles