Question

I have to keep in memory a predefined number of images. I'have created an array like this :

int nb_frame_decalage=10;
IplImage** accumulateur_image;
CvCapture* capture;
IplImage* Image_B_Brut;


capture=cvCreateFileCapture("./Data/video2.avi");
if (!capture) {printf("\nOuverture du flux vidéo impossible !\n");key='k';}
image_B_brut = cvQueryFrame(capture);


accumulateur_image=malloc(sizeof(IplImage*)*nb_frame_decalage);
int i;
for (i=0;i<nb_frame_decalage;i++)
{
    accumulateur_image[i]=cvCreateImage(cvGetSize(image_B_brut),
    image_B_brut->depth,
    image_B_brut->nChannels);
}
add_image(&accumulateur_image,image_B_brut,nb_frame_decalage);

I loop on cvCapture after that, but it give an error in the first call of this function : I want to add Element to the end of this list...

void add_image(IplImage*** accumulateur,IplImage* Image_to_add,int taille_accumulateur)
{
    int i;
    for (i=0;i<taille_accumulateur;i++)
    {
        cvCopy((*accumulateur)[i+1],(*accumulateur)[i],NULL);
    }
    cvCopy(Image_to_add,*accumulateur[taille_accumulateur],NULL);
}

It compile but it seems to be a problem in the add_image function, because a segmentation fault appear when the program come in this function.

Does someone already have this mistake ?

EDIT AFTER ANSWER

Finaly, this code works, i modified index add_image function.

int nb_frame_decalage=10;
IplImage** accumulateur_image;
CvCapture* capture;
IplImage* Image_B_Brut;


capture=cvCreateFileCapture("./Data/video2.avi");
if (!capture) {printf("\nOuverture du flux vidéo impossible !\n");key='k';}
image_B_brut = cvQueryFrame(capture);


accumulateur_image=malloc(sizeof(IplImage*)*nb_frame_decalage);
int i;
for (i=0;i<nb_frame_decalage;i++)
{
    accumulateur_image[i]=cvCreateImage(cvGetSize(image_B_brut),
    image_B_brut->depth,
    image_B_brut->nChannels);
}
add_image(&accumulateur_image,image_B_brut,nb_frame_decalage);


void add_image(IplImage*** accumulateur,IplImage* Image_to_add,int taille_accumulateur)
{
    int i;
    for (i=0;i<taille_accumulateur-1;i++)
    {
        cvCopy((*accumulateur)[i+1],(*accumulateur)[i],NULL);
    }
    cvCopy(Image_to_add,(*accumulateur)[taille_accumulateur-1],NULL);
}

Thanks to alk for his helpful answer.

Was it helpful?

Solution

Assuming you call add_image() like this

add_image(accumulateur_image, ..., nb_frame_decalage);

then the following line access *accumulateur out of bounds:

  cvCopy((*accumulateur)[i+1], ... /* for i=taille_accumulateur */

Accessing an array out of bounds provokes undefinded behaviour, which in many cases result in a segmentation violation aka segfault.


Also this

cvCopy(Image_to_add,*accumulateur[taille_accumulateur],NULL);

shall be

cvCopy(Image_to_add, (*accumulateur)[taille_accumulateur], NULL);

as the [] operator binds tighter then the * operator. But even if fixed as shown the code might access *accumulateur out of bounds under the assumption made above.

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