Question

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!

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top