Ufuk Can Bicici
Ufuk Can Bicici

Reputation: 3649

OpenCV: Is using the same Mat object both as source and destination safe?

I have the following question about OpenCV; I always find the memory management of OpenCV more or less weird and therefore I am not very sure if the following operation is safe (does not cause any dangling pointers, memory leaks etc).

I have a Mat object, storing an image in it and I want to resize it, using the OpenCV function, resize.

I want to use the function as in the following:

resize(image,image,Size(),paramStruct.upScaleRatio,paramStruct.upScaleRatio, INTER_LANCZOS4);

I am using the source object as the destination as well. How exactly behaves OpenCV in that case, does it safely free the old data in the image object after resizing it (most likely into a temporary object)? Or should I always use a new Mat object, different than the source? (I would not prefer that if possible since it complicates the code)

Upvotes: 4

Views: 1067

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150785

It always create new Mat object since one cannot resize in-place. The image object can be thought of as an address to image.data. When you do

image = temp_img 

it actually only transfers the overheads. So the operation is minimal.

When temp_imp is a local variable (like in resize) it is freed after the function exits.

Upvotes: 2

Related Questions