2013-03-28 156 views

回答

26

這是Mat::clone()函數的實現:

inline Mat Mat::clone() const 
{ 
    Mat m; 
    copyTo(m); 
    return m; 
} 

所以,@rotating_image曾提到,如果你不爲copyTo()功能提供mask,這是一樣的clone()

17

Mat::copyTo適用於當您已經有一個目的地cv::Mat(可能是)已經分配了正確的數據大小。 Mat::clone是一個方便,當你知道你必須分配一個新的cv::Mat

26

其實他們是不是即使沒有面具也一樣。

主要區別在於,當目標矩陣和源矩陣具有相同的類型和大小時,copyTo將不會更改目標矩陣的地址,而clone將始終爲目標矩陣分配新地址。

當在copyToclone之前使用複製賦值運算符複製目標矩陣時,這很重要。例如,

使用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]