从 OpenCV 文档来看,复制矩阵似乎是使用浅拷贝完成的,但是当更改其中一个拷贝时,就会完成一个拷贝。
确切的 reference是:
Mat& Mat::operator = (const Mat& m) Mat& Mat::operator = (const MatExpr_Base& expr) Mat& operator = (const Scalar& s)
Matrix assignment operators
Parameters:
m – The assigned, right-hand-side matrix. Matrix assignment is O(1) operation, that is, no data is copied. Instead, the data is shared and the reference counter, if any, is incremented. Before assigning new data, the old data is dereferenced via Mat::release .
expr – The assigned matrix expression object. As opposite to the first form of assignment operation, the second form can reuse already allocated matrix if it has the right size and type to fit the matrix expression result. It is automatically handled by the real function that the matrix expressions is expanded to. For example, C=A+B is expanded to cv::add(A, B, C) , and add() will take care of automatic C reallocation.
s – The scalar, assigned to each matrix element. The matrix size or type is not changed.
但是,这似乎不起作用
Mat_<float> a(5,5),b(5,5);
a =1;
b = a;
a = 2;
现在 b == 2,而不是 1
最佳答案
看来你误会了。 “在分配新数据之前,旧数据通过 Mat::release 取消引用” 并不意味着当您在 a
或 b
上写入时发生复制。这意味着当您键入 b=a
时,您会丢失 b 中的数据。
长话短说:不支持写入时复制。
关于c++ - OpenCV Mat::operator= - 它支持写时复制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411476/