OpenCV Mat :: operator = - 它支持写入时复制吗? [英] OpenCV Mat::operator= - does it support copy on write?

查看:180
本文介绍了OpenCV Mat :: operator = - 它支持写入时复制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从OpenCV文档看来,复制矩阵是使用浅拷贝完成的,但是当更改其中一个拷贝时,会执行一个拷贝。



确切的参考是:


  Mat& Mat :: operator =(const Mat& m)

Mat& Mat :: operator =(const MatExpr_Base& expr)

Mat& operator =(const Scalar& s)

矩阵赋值运算符



参数:



m - 分配的右侧
矩阵。矩阵赋值是O(1)
操作,也就是说,没有数据被复制。
相反,数据是共享的,
引用计数器(如果有)是
递增。 在分配新的
数据之前,旧数据通过
Mat :: release
取消引用。



expr - 矩阵表达式
对象。与赋值操作的第一个形式
相反,第二个
形式可以重用已分配的
矩阵(如果它具有正确的大小)和
类型以适合矩阵表达式
结果。它由
自动处理矩阵
表达式扩展到的实函数。对于
示例,C = A + B扩展为
cv :: add(A,B,C),add()将自动C重新分配
。 >

s - 标量,分配给每个
矩阵元素。


但是,这看起来不起作用

  Mat_< float> a(5,5),b(5,5); 
a = 1;
b = a;
a = 2;

现在b == 2,intead 1

解决方案

看来你误解了。 在分配新数据之前,通过Mat :: release取消引用旧数据并不意味着在上书写 b ,则会发生复制。这意味着当您键入 b = a 时,您将丢失b中的数据。



:不支持copy on write。


From the OpenCV documentation, it appears that copying a matrix is done using a shallow copy, but when changing one of the copies, a copy is done.

The exact reference is:

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.

However, this appears not to work

Mat_<float> a(5,5),b(5,5);
a =1;
b = a;
a = 2;

now b == 2, intead of 1

解决方案

It seems you misunderstood. "Before assigning new data, the old data is dereferenced via Mat::release" does not mean that when you write on a or b then a copy occurs. It means that when you type b=a, you lose the data that was in b.

Long story short : copy on write is not supported.

这篇关于OpenCV Mat :: operator = - 它支持写入时复制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆