我知道'copyTo'可以处理面具。但是当不需要面膜时,我可以同时使用它吗?
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-clone
答案 0 :(得分:47)
实际上,即使没有面具,它们 NOT 也是一样。
主要区别在于,当目标矩阵和源矩阵具有相同的类型和大小时,copyTo
将不会更改目标矩阵的地址,而clone
将始终分配新地址目的地矩阵。
在copyTo
或clone
之前使用复制赋值运算符复制目标矩阵时,这很重要。例如,
使用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]
答案 1 :(得分:28)
这是Mat::clone()
函数的实现:
inline Mat Mat::clone() const
{
Mat m;
copyTo(m);
return m;
}
因此,正如@rotating_image所提到的,如果你没有为mask
函数提供copyTo()
,那么它与clone()
相同。
答案 2 :(得分:20)
Mat::copyTo
适用于您已经拥有(可能是或)已经分配了正确数据大小的目标cv::Mat
的情况。当您知道必须分配新的Mat::clone
时,cv::Mat
便于使用。
答案 3 :(得分:0)
copyTo不会在堆中分配更快的新内存。