0

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:

EDIT: 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;

Here is the get function:

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

Here is the put function:

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.

If I then try to do this:

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

}

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

NOP
  • 864
  • 1
  • 12
  • 26

1 Answers1

0

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

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;
}
snugglo
  • 259
  • 1
  • 5
  • But it definitely compiles though...so what's up with that? – NOP Mar 16 '12 at 07:24
  • I've updated my question...I couldn't work out what was going on, so I started from scratch but now I'm having type issues that I can't seem to track down. – NOP Mar 16 '12 at 17:08
  • 1
    In your struct I see the array of pointers to IplImages IplImage* queue[BUFFER_SIZE]; Do you initialize all those images? I mean if you do create them yourself or get them from somewhere? You might have an allocation problem if they are not properly initialized which could result in the error in cvGetMat() – snugglo Mar 19 '12 at 19:37