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

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

问题描述

我有一个关于 OpenCV 中的 cvQueryFrame() 的基本问题.

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

我有以下代码:

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

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

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?

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

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.

提前致谢

我从谷歌发现调用 cvQueryFrame() 两次返回相同的指针.现在我有点困惑.如果我在 while 循环中只使用一次调用它,帧会进步,但如果我调用它两次就不会?有没有简单的方法可以抓取两帧?

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() 返回指向 OpenCV 的私有"内部缓冲区的指针,它总是用最后一个抓取的帧填充.
如果您想要 2 帧,则需要缓存一个副本.为前一帧分配(例如,使用 cvCloneImage())空间后,您可以使用 cvCopy() 仅复制图像数据.不要在循环内使用 cvCloneImage() 因为由于内部内存分配(和释放,否则你也会有内存泄漏),这非常低效.

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).

更新:代码如下所示:

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 );

注意:通常,您可以通过进行转换来避免这种冗余"复制时间.
例如,假设您的显示器是彩色的,但您的处理是灰度的.您只需要灰度的 2 个连续副本.由于您无论如何都需要转换为灰度,因此您可以直接从捕获的帧中执行此操作,从而避免多余的 cvCopy().要保存前一帧,您只需在 2 个分配的灰度图像之间交换指针即可.

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天全站免登陆