c++ - OpenCV Mat::operator= - 它支持写时复制吗?

标签 c++ opencv

从 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 取消引用” 并不意味着当您在 ab 上写入时发生复制。这意味着当您键入 b=a 时,您会丢失 b 中的数据。

长话短说:不支持写入时复制。

关于c++ - OpenCV Mat::operator= - 它支持写时复制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6411476/

相关文章:

C++11 STL 独有的函数,但反过来

java - Android 的 OpenCV "Hello World"示例

opencv - 有没有一种方法可以确定同时保留原始信息的图像中的噪声水平?

python - OpenCV cv2 图像到 PyGame 图像?

iphone - 无法将相机 View 校准为 3D 模型

使用 contrib-modules 安装 Opencv 3.0.0

c++ - 这与字节序有什么关系吗?

c++ - 这些代码之间有什么区别 :one of them does'nt change label text

c++ - 构造函数被隐式删除,因为其异常规范与隐式异常规范不匹配

c++ - 在 STL 列表中输出一个元组