Search code examples
cpointersopencviplimage

OpenCV: Buffering IplImage*'s to share between pthreads


I've been grinding at trying to get a circular buffer working for days, and it's just not playing ball for some reason. I'm writing the program in C, not C++.

I'm trying to buffer images from a camera in one thread, so it can be read and processed in another thread (I know about how to use pthread_mutex_* for resource locking). So far, I've managed buffer images onto my buffer but I'm having issues retrieving them. This is my circular buffer implementation:

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

static frame_buffer frbuff;

void buff_init()
{
    frbuff.in = &frbuff.queue[0];
    frbuff.out = &frbuff.queue[0];
    frbuff.num_frames = 0;
    frbuff.in_ctr = 0;
    frbuff.out_ctr = 0;
    frbuff.update_flag = 0;
}

int buff_size()
{
    return frbuff.num_frames;
}

int buff_flag_check()
{
    return frbuff.update_flag;
}

void buff_flag_set()
{
    frbuff.update_flag = 1;
}

void buff_flag_clr()
{
    frbuff.update_flag = 0;
}

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;
    buff_flag_clr();
    return nextfr;
}

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;
    }
    buff_flag_set();
    return 1;
}

I can buff_put() just fine, this appears to work as I'm seeing the counter increase. When I buff_get(), I also get something that's not null. The issue comes when I try to do anything with it:

IplImage **some_image;
some_image = buff_get();
cvShowImage("some_window_somewhere",some_image);

Well apparently it's unhappy with that...

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 also tried this (which results in a segmentation fault):

cvShowImage("some_window_somewhere",*some_image);

I'm very stuck, have tried everything I can think of. No doubt it will end up being something obvious (I hate pointers). Any help would be greatly appreciated.


Solution

  • I am going to make an assumption here and assuming you are trying to build a queue of ImpImage*'s. If that is the case, this line:

    IplImage** queue[BUFFER_SIZE];
    

    should read:

    IplImage* queue[BUFFER_SIZE];
    

    And these lines:

    IplImage** buff_get()
    {
        IplImage** nextfr;
        if(frbuff.num_frames == 0)
        {
            return NULL;
        }
        nextfr = frbuff.out++;
    

    should read:

    IplImage* buff_get()
    {
        IplImage* nextfr;
        if(frbuff.num_frames == 0)
        {
            return NULL;
        }
        nextfr = *frbuff.out++;
    

    And these lines:

    IplImage **some_image;
    some_image = buff_get();
    cvShowImage("some_window_somewhere",some_image);
    

    should read:

    IplImage *some_image;
    some_image = buff_get();
    cvShowImage("some_window_somewhere",some_image);
    

    Next we have a straightforward bug. This line:

    frbuff.in = nextfr;
    

    should read:

    *frbuff.in = nextfr;
    

    I am surprised a modern C compiler didn't reject the code outright. At the very least it must be giving a warnings. If you are using gcc I suggest passing -Wall -Wextra as a minimum for all new code you write. Getting your code into a form the compiler accepts without warnings may be painful, but in the long run as you are discovering not doing that is even more painful.

    Secondly, you say you are using pthreads. Most of your code will be OK if buf_get() is always called from the same thread, and likewise for buf_put(). If buf_get() is called from more than one thread then at the very least your queue will be corrupted, and ditto for buf_put(). The one exception to all this is frbuff.update_flag which is altered from both buf_get() and buf_put(). If buf_get() and buf_put() are called from different threads you can be assured update_flag will end up containing the wrong thing.