Ask Your Question
0

Initialize the images. Problems with the memory.

asked 2013-10-22 05:21:24 -0500

Candela gravatar image

updated 2013-10-22 05:22:13 -0500

Hello,

I am implementing some methods and i'm using OpenCv functions.

I work with many frames of a video and i need to implement the best code to avoid some problems with the memory. I have some doubts:

Doubt 1: How would it be better?

- Option 1:

IplImage* image1 = NULL;

...

IplImage* picture_sintetica(.. ){
    ...
    if (image1 == NULL){
        image1 = cvCreateImage( cvSize(w_i/2, h_i), IPL_DEPTH_8U, 3);
    }
    ...
}

- Option 2:

IplImage* image1 = NULL;

...

IplImage* picture_sintetica(.. ){
    ...
    image1 = cvCreateImage( cvSize(w_i/2, h_i), IPL_DEPTH_8U, 3);

    ...
    cvReleaseImage(&image1);
}

I think that in option 2, the image1 is created many times(each time the method named picture_sintetica is called) and with the option 1 it will be created only once, but I'm not sure...and in other examples i've seen using the option 2.

Doubt 2: Is it equivalent declare the image equal to zero (IplImage* image1 = 0;), to NULL (IplImage* image1 = NULL;) o to put anything (IplImage* image1;)?

Doubt 3: When is it recommendable to use the function named cvCloneImage and when is it better to use cvCopy?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-10-22 09:11:22 -0500

In C++, the definition of NULL is 0.

When you dereference a pointer, it is your responsibility to make sure it points somewhere valid. A common solution is to set all pointers as NULL (if they don't point to anything). Then, you check for that before accessing the pointer. It is common to set them to NULL when you delete or free() them.

In opencv > 2.0 Mat::clone calls Mat::copyTo. If you don't provide a mask for copyTo() function, it's the same as clone(). So i think, it's the same idea behind cvCopy and cvCloneImage.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-10-22 05:21:24 -0500

Seen: 537 times

Last updated: Oct 22 '13