OpenCV submatrix access: copy or reference?

S.H*_*S.H 7 c++ opencv copy reference matrix

If I extract a submatrix from a matrix using

cv::Mat A = cv::Mat::ones(4,4);

cv::Mat B = A( cv::Rect( 1, 1, 2, 2 ) );
Run Code Online (Sandbox Code Playgroud)

Is "B" a copy of those values from "A" or does it reference to those values?

Could you provide an example of how to get

(1)子矩阵的副本?

(2)对子矩阵的引用?

ber*_*rak 14

B是A的Mat-header的副本,但引用相同的像素.

所以,如果你操纵B的像素,A也会受到影响.

(1)('深拷贝')将是:

cv::Rect r( 1, 1, 2, 2 );
cv::Mat A = cv::Mat::ones(4,4);
cv::Mat B = A(r).clone(); // now B has a seperate *copy* of the pixels

cv::Mat C; 
A(r).copyTo(C);           // another way to make a 'deep copy'
Run Code Online (Sandbox Code Playgroud)

(2)(一个"浅拷贝"),这就是你上面所做的:

cv::Mat B = A(r);         // B points to A's pixels
Run Code Online (Sandbox Code Playgroud)