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
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With