0

I'm using dynamic array of IplImage type for storing some images (I would like to do the same thing with cvHistogram but get the same error) from which I need to extract histogram data. Unfortunately I'm getting the error and have no knowledge how to solve it. Any help and suggestions to do this another way would be appreciated.

This is the part of the code:

void getColorHistogram( void ){

    IplImage *images = (IplImage *)malloc( sizeof(IplImage) * 6 );

    if ( images == NULL )
    {
        printf("Memory error. EXITING...\n");
        exit( -1 );
    }

    for (int i = 0; i < 6 ; i++ ){

        char *num = (char *)malloc( sizeof(int) );
        char *extension = (char *)".jpg";
        sprintf( num, "%d", i );

        int nameLen = strlen( num ) + strlen( extension ) + 1;

        char *imgName = (char *)malloc( nameLen );
        strlcpy( imgName, num, nameLen );
        strlcat( imgName, extension, nameLen );

        images[i] = cvLoadImage( imgName, CV_LOAD_IMAGE_UNCHANGED );
    }

    free( images );

}

And this is the error that I get

    error: no match for ‘operator=’ in ‘images[i] = cvLoadImage
(((const char*)imgName), -0x00000000000000001)’
    /opt/local/include/opencv2/core/types_c.h:463: note: 
candidates are: _IplImage& _IplImage::operator=(const _IplImage&)

P.S. I'm using i<6 in loop because sizeof(images)/sizeof(images[0]) gives me 0.

Many thanks!

Moirae
  • 139
  • 3
  • 14

2 Answers2

1

This is a C++ compile I assume. The function call is returning a const pointer and your array isnt a list of const pointers.

ojblass
  • 21,146
  • 22
  • 83
  • 132
0

The problem is that you are trying to assign the IplImage* returned from cvLoadImage() to the IplImage dereferenced from images[i]. You cannot convert between the two types.

A quick fix for this could be to dereference the pointer returned from cvLoadImage(), like so:

images[i] = *cvLoadImage( imgName, CV_LOAD_IMAGE_UNCHANGED );

This might work, but it seems a little unorthodox to me.

A better solution would be to not dynamically allocate the IplImage yourself, but instead keep an array of IplImage* on the stack:

#define NUM_IMAGES 6;
IplImage *images[NUM_IMAGES];

for (int i = 0; i < NUM_IMAGES ; i++ ){

    char *num = (char *)malloc( sizeof(int) );
    char *extension = ".jpg";
    sprintf( num, "%d", i );

    int nameLen = strlen( num ) + strlen( extension ) + 1;

    char *imgName = (char *)malloc( nameLen );
    strlcpy( imgName, num, nameLen );
    strlcat( imgName, extension, nameLen );

    images[i] = cvLoadImage( imgName, CV_LOAD_IMAGE_UNCHANGED );
}

/* Release the image memory */
for (size_t i = 0; i < NUM_IMAGES; ++i)
{
    cvReleaseImage(&images[i]);
}

If for some reason you must dynamically allocate, then do it with pointers instead. Replace the first line of my example with:

IplImage** images = (IplImage**)malloc(sizeof(IplImage*) * 6);

and then place a free(images); at the very end.

As a side note, if at all possible, consider switching to the C++ API. You will save yourself so much pain.

Community
  • 1
  • 1
Aurelius
  • 11,111
  • 3
  • 52
  • 69
  • Thank you for your answer. Initially I went this way and had some troubles with return from this function. Then I asked a friend for help and he advised me to switch to dynamic allocation. I will use this, and unfortunately I cannot switch to C++ API ;) Thanks again! – Moirae Jul 04 '13 at 10:37
  • Another question now would be if I use this method how to return array of IplImage? – Moirae Jul 04 '13 at 11:05
  • Take a look at [this question](http://stackoverflow.com/q/1453410/1601291). If you have to return the images, I would use the `IplImage**` approach. – Aurelius Jul 04 '13 at 17:27