简体   繁体   English

Opencv 从网络摄像头捕获的连续帧

[英]Opencv consecutive frames capture from webcam

I'm developing a realtime opticalflow app with Opencv (C++).我正在使用 Opencv (C++) 开发一个实时光流应用程序。 I don't understand how capture two consecutive frames to apply Lucas-Kanade Tracking method.我不明白如何捕获两个连续帧以应用 Lucas-Kanade 跟踪方法。

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. cvQueryFrame 每次都重用相同的缓冲区。 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. 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.我总是遇到这种情况0xC0000005 i wonder that is there anything wrong in my code.我想知道我的代码有什么问题。 I use image = _image , because I think that _image = cvQueryFrame(capture);我使用image = _image ,因为我认为_image = cvQueryFrame(capture); is just point to the buffer of capture, so I can save the frame in another memory situation.只是指向捕获的缓冲区,所以我可以在另一个 memory 情况下保存帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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