简体   繁体   中英

Opencv consecutive frames capture from webcam

I'm developing a realtime opticalflow app with Opencv (C++). I don't understand how capture two consecutive frames to apply Lucas-Kanade Tracking method.

This don't work:

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

I also have 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 isn't a stupid question, but I suspect that it is:/ Sorry in advance. Thanks!!

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.

I explain why original code does not work. cvQueryFrame reuses same buffer each time. So when you do:

IplImage *imgA=cvQueryFrame( capture );

you get pointer to his internal buffer where image is held. Now when doing:

IplImage *imgB=cvQueryFrame( capture );

image is rewritten and pointer to same buffer is given. ImgA==ImgB. You have to make copy after querying first frame, then everything works.

one query before the loop, and another inside the loop might be enough here (pseudocode):

IplImage prev = query(capture)

while(1)
   next = query(capture )

   opticalflow( prev, next )

   prev = cvCopy( next )
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. i wonder that is there anything wrong in my code. I use image = _image , because I think that _image = cvQueryFrame(capture); is just point to the buffer of capture, so I can save the frame in another memory situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM