Mat :: clone和Mat :: copyTo有什么区别? [英] What's the difference between Mat::clone and Mat::copyTo?
问题描述
我知道'copyTo'可以处理遮罩.但是当不需要口罩时,我可以同时使用吗?
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
推荐答案
实际上,即使没有遮罩,它们也是不相同.
Actually, they are NOT the same even without mask.
主要区别在于,当目标矩阵和源矩阵具有相同的类型和大小时,copyTo
不会更改目标矩阵的地址,而clone
始终会为目标矩阵分配新的地址
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.
当在copyTo
或clone
之前使用复制分配运算符复制目标矩阵时,这一点很重要.例如,
This is important when the destination matrix is copied using copy assignment operator before copyTo
or clone
. For example,
使用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;
输出:
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
使用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;
输出:
[0, 0, 0, 0, 0]
[1, 1, 1, 1, 1]
这篇关于Mat :: clone和Mat :: copyTo有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!