"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."
以上文字参考自https://stackoverflow.com/questions/15672600/whats-the-difference-between-matclone-and-matcopyto
简单解释下,当目标矩阵与源矩阵具有相同的type和size时,copyTo不会为目标矩阵重新分配内存,而clone总是会为目标矩阵重新分配内存。以下两段代码很好地解释了两者之间的区别:
Mat mat1 = Mat::ones(1, 5, CV_32F); // [1,1,1,1,1]
Mat mat2 = mat1; // mat2与mat1指向同一内存地址
Mat mat3 = Mat::zeros(1, 5, CV_32F); // [0,0,0,0,0]
mat3.copyTo(mat1); // mat1未被重新分配内存,通过mat1可以改变mat2的内容
cout << mat1 << endl; // [0,0,0,0,0]
cout << mat2 << endl; // [0,0,0,0,0]
Mat mat1 = Mat::ones(1, 5, CV_32F); // [1,1,1,1,1]
Mat mat2 = mat1; // mat2与mat1指向同一内存地址
Mat mat3 = Mat::zeros(1, 5, CV_32F); // [0,0,0,0,0]
mat1 = mat3.clone(); // mat1被重新分配内存,通过mat1不能改变mat2的内容
cout << mat1 << endl; // [0,0,0,0,0]
cout << mat2 << endl; // [1,1,1,1,1]