C + OpenCV:具有循环缓冲区的IplImage [英] C + OpenCV: IplImage with circular buffer

查看:149
本文介绍了C + OpenCV:具有循环缓冲区的IplImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个循环缓冲区,以用作OpenCV(使用C)的帧缓冲区.

I'm trying to implement a circular buffer for use as a frame buffer with OpenCV (using C).

为了防止重蹈覆辙,我从这篇帖子中偷偷地偷走了循环缓冲区实现:

I've shamelessly stolen the circular buffer implementation from this post to save reinventing the wheel:

好的,所以我重新定义了几件事.即,我实现了自己的循环缓冲区.现在我收到了没有道理的错误.

OK, so I've redefined a few things. Namely I implemented my own circular buffer. Now I'm getting errors which don't make sense.

这是我正在使用的循环缓冲区实现:

Here is the circular buffer implementation I'm using:

#define BUFFER_SIZE 100

typedef struct
{
    IplImage* queue[BUFFER_SIZE];
    IplImage *in;
    IplImage *out;
    int num_frames;
    int in_ctr;
    int out_ctr;
    int update_flag;
} frame_buffer;

这是get函数:

IplImage* buff_get()
{
    IplImage* nextfr;
    if(frbuff.num_frames == 0)
    {
        return NULL;
    }
    nextfr = frbuff.out++;
    if(++frbuff.out_ctr == BUFFER_SIZE)
    {
        frbuff.out = &frbuff.queue[0];
        frbuff.out_ctr = 0;
    }
    --frbuff.num_frames;
    return nextfr;
}

这是put函数:

int buff_put(IplImage* nextfr)
{
    if(++frbuff.num_frames > BUFFER_SIZE)
    {
       return 0;
    }
    frbuff.in++; 
    frbuff.in = nextfr;
    if(++frbuff.in_ctr == BUFFER_SIZE)
    {
        frbuff.in = &frbuff.queue[0];
        frbuff.in_ctr = 0;
    }

    return 1;
}

一切似乎都顺利.帧出现在缓冲区中,我知道这是因为我可以打印出尺寸.但是,当我尝试显示缓冲区中的图像时,一切都变糟了.

Everything seems to go OK. Frames appear on the buffer, which I know because I can print the size out. But it all goes bad when I try to show the image that's on the buffer.

如果我再尝试这样做:

IplImage* curr_frame = cvCreateImage(cvSize(640,480),8,3);
cvNamedWindow("proc_window",CV_WINDOW_AUTOSIZE);
cvShowImage("proc_window",curr_frame);

while(1)
{     
   if(buff_size() > 0) 
   {    
        if(buff_flag_check()) curr_frame = buff_get();
        if(curr_frame != NULL)
        {
            cvShowImage("proc_window",curr_frame);
        }

}

调用cvShowImage()时出现以下错误:

I recieve the following error upon calling cvShowImage():

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/fagg/src/OpenCV-2.3.1/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
what():  /home/fagg/src/OpenCV-2.3.1/modules/core/src/array.cpp:2482: error: (-206)     Unrecognized or unsupported array type in function cvGetMat

我对这里发生的事情很困惑.希望比我眼睛更新鲜的人可以看到发生了什么...

I'm quite confused as to what's going on here. Hopefully someone with fresher eyes than myself can see what's going on...

推荐答案

您提供的代码段不会编译,因为cb_init()将指向circle_buffer的指针作为第一个参数.

The snippet of code you provided won't compile as cb_init() takes a pointer to a circular_buffer as first argument.

如果在cb_init()中遇到段错误,那是因为malloc()无法返回请求的大小,并且链接中的实现无法处理错误.

and if you get a segfault in cb_init() it is because malloc() fails to return the requested size and the implementation in the link does not handle errors.

void cb_init(circular_buffer *cb, size_t capacity, size_t sz)
{
    cb->buffer = malloc(capacity * sz);
    if(cb->buffer == NULL)
        // handle error
    cb->buffer_end = (char *)cb->buffer + capacity * sz;//segfault when using cb->buffer which is null in case of malloc() failure
    cb->capacity = capacity;
    cb->count = 0;
    cb->sz = sz;
    cb->head = cb->buffer;
    cb->tail = cb->buffer;
}

这篇关于C + OpenCV:具有循环缓冲区的IplImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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