Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Mat::clone and Mat::copyTo?

Tags:

opencv

I know 'copyTo' can handle mask. But when mask is not needed, can I use both equally?

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-clone

like image 923
yosei Avatar asked Mar 28 '13 01:03

yosei


People also ask

Which function is used to to clone an image with a mask?

copyTo() function does not clear the output before copying. If you want to permanently alter the original Image, you have to do an additional copy/clone/assignment. The copyTo() function is not defined for overlapping input/output images. So you can't use the same image as both input and output.

What is mat in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc. This class comprises of two data parts: the header and a pointer.


1 Answers

Actually, they are NOT the same even without mask.

The major difference is that when the destination matrix and the source matrix have the same type and size, copyTo will not change the address of the destination matrix, while clone will always allocate a new address for the destination matrix.

This is important when the destination matrix is copied using copy assignment operator before copyTo or clone. For example,

Using copyTo:

Mat mat1 = Mat::ones(1, 5, CV_32F); Mat mat2 = mat1; Mat mat3 = Mat::zeros(1, 5, CV_32F); mat3.copyTo(mat1); cout << mat1 << endl; cout << mat2 << endl; 

Output:

[0, 0, 0, 0, 0] [0, 0, 0, 0, 0] 

Using clone:

Mat mat1 = Mat::ones(1, 5, CV_32F); Mat mat2 = mat1; Mat mat3 = Mat::zeros(1, 5, CV_32F); mat1 = mat3.clone(); cout << mat1 << endl; cout << mat2 << endl; 

Output:

[0, 0, 0, 0, 0] [1, 1, 1, 1, 1] 
like image 103
yangjie Avatar answered Oct 02 '22 12:10

yangjie