使用cvQueryframe读取连续帧OpenCV [英] read successive frames OpenCV using cvQueryframe

查看:977
本文介绍了使用cvQueryframe读取连续帧OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对OpenCV中的cvQueryFrame()有一个基本问题。



我有以下代码:



  IplImage * frame1,* frame2; 
frame1 = cvQueryFrame(capture);
frame2 = cvQueryFrame(capture);



现在我的问题是:if frame1 是指向第一帧的指针,是 frame2 指向第二帧的指针?那么两个 cvQueryFrame()调用会读取连续的帧吗?



我以为我会先检查自己,但是指针 frame1 frame2 似乎具有相同的十六进制值。 :s我只需要一次捕获两个帧然后需要处理它们。



提前致谢



<编辑:我从谷歌发现,调用cvQueryFrame()两次返回相同的指针。现在我有点困惑。如果我在while循环中只使用一次调用它,帧会进展,但如果我调用它两次则没有?是否有一种简单的方法来抓取两个帧?

解决方案

cvQueryFrame()返回指向OpenCV的私有内部缓冲区的指针,它总是填充最后一个抓取的帧。

如果你想要2帧,你需要缓存一个副本。一旦为前一帧分配(例如使用 cvCloneImage())空间,就可以使用 cvCopy()进行复制只是图像数据。 在循环中使用 cvCloneImage()因为内部内存分配(和解除分配,否则你会有内存)非常低效泄漏也是如此)。



更新:代码如下所示:

  IplImage * currFrame = 0; 
IplImage * prevFrame = 0;
CvCapture * cap = cvCaptureFromAVI(sample.avi);
currFrame = cvQueryFrame(cap);

//克隆框架以具有相同大小和类型的副本
prevFrame = cvCloneImage(currFrame);

while(currFrame = cvQueryFrame(cap))
{
//使用currFrame和prevFrame处理视频...

// .. 。


//完成后,使用当前副本覆盖prevFrame,为下一帧准备
//。
cvCopy(currFrame,prevFrame);
}

cvReleaseImage(& img1);
cvReleaseCapture(& cap);

注意:通常,您可以通过以下方式避免此冗余复制时间改为做转换。

例如,假设您的显示器是彩色的,但您的处理是灰度的。您只需要连续2个灰度副本。由于您无论如何都需要转换为灰度,您可以直接从捕获的帧中执行此操作,从而避免冗余的 cvCopy()。要保存前一帧,您只需在2个分配的灰度图像之间交换指针。


I have a basic question in regards to cvQueryFrame() in OpenCV.

I have the following code:

IplImage *frame1,*frame2;
frame1 = cvQueryFrame(capture);
frame2 = cvQueryFrame(capture);

Now my question is: if frame1 is a pointer to the first frame, is frame2 a pointer to the 2nd frame? So will the two cvQueryFrame() calls read successive frames?

I thought I'd check myself first but the pointers frame1,frame2 seem to have the same hex value. :s I just need to capture two frames at a time and then need to process them.

Thanks in advance

EDIT: I found from google that calling cvQueryFrame() twice returns the same pointer. now I am slightly confused. If i use call it only once in a while loop, the frames progress but not if i call it twice? Is there a easy way to grab two frames?

解决方案

cvQueryFrame() returns the pointer to OpenCV's "private" internal buffer which it always fills with the last grabbed frame.
If you want 2 frames you will need to cache a copy. Once you allocate (e.g. using cvCloneImage()) space for your previous frame, you can use cvCopy() to copy just the image data. Do not use cvCloneImage() inside the loop as that is very inefficient due to the internal memory allocations (and deallocations, otherwise you'll have memory leaks as well).

Update: the code will look something like this:

IplImage* currFrame = 0;
IplImage* prevFrame = 0;
CvCapture* cap = cvCaptureFromAVI("sample.avi");   
currFrame = cvQueryFrame( cap );

 // Clone the frame to have an identically sized and typed copy
prevFrame  = cvCloneImage( currFrame );

while(currFrame = cvQueryFrame( cap ))
{
    // process the video using currFrame and prevFrame...

    // ...


    // When done, overwrite prevFrame with current copy in preparation
    // for the next frame.
    cvCopy( currFrame , prevFrame); 
} 

cvReleaseImage( &img1 );
cvReleaseCapture( &cap );

Note: Often, you can avoid this "redundant" copying time by doing a conversion instead.
For example, say your display is in color, but your processing is in grayscale. You only need the 2 consecutive copies in grayscale. Since you will need to convert to grayscale anyway, you can do that directly from the captured frame thus avoiding the redundant cvCopy(). To save the previous frame you would just swap pointers between your 2 allocated grayscale images.

这篇关于使用cvQueryframe读取连续帧OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆