这是一个参考,除非您明确复制数据。
为此,可以使用一个小例子看看:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Rect r(0,0,2,2);
Mat1b mat(3,3, uchar(0));
cout << mat << endl;
// mat
// 0 0 0
// 0 0 0
// 0 0 0
Mat1b submat_reference1(mat(r));
submat_reference1(0,0) = 1;
cout << mat << endl;
cout << submat_reference1 << endl;
// mat
// 1 0 0
// 0 0 0
// 0 0 0
// submat_reference1
// 1 0
// 0 0
Mat1b submat_reference2 = mat(r);
submat_reference2(0, 0) = 2;
cout << mat << endl;
cout << submat_reference1 << endl;
cout << submat_reference2<< endl;
// mat
// 2 0 0
// 0 0 0
// 0 0 0
// submat_reference1 = submat_reference2
// 2 0
// 0 0
Mat1b submat_deepcopy = mat(r).clone();
submat_deepcopy(0,0) = 3;
cout << mat << endl;
cout << submat_reference1 << endl;
cout << submat_reference2 << endl;
cout << submat_deepcopy << endl;
// mat
// 2 0 0
// 0 0 0
// 0 0 0
// submat_reference1 = submat_reference2
// 2 0
// 0 0
// submat_deepcopy
// 3 0
// 0 0
return 0;
}