标签:des style color io strong 数据 for ar
当我们想要将一个Mat对象的数据复制给另一个Mat对象时,应该怎么做呢?
我们发现,OpenCV提供了重载运算符Mat::operator = ,那么,是否按照下列语句就可以轻松完成对象的赋值呢?
Mat a; Mat b = a;答案是否定的!
我们可以从reference manual 中看到:
Mat::operator =
Provides matrix assignment operators.
C++: Mat& Mat::operator=(const Mat& m)
Parameters
m – Assigned, right-hand-side matrix. Matrix assignment is an O(1) operation.
This means that no data is copied but the data is shared and the reference counter, if any, is incremented. Before assigning new data, the old data is de-referenced via Mat::release() .
这意味着,操作之后,b与a将共享同一片数据,不会发生数据的复制。
同样,我们可以从源代码中看出:
inline Mat& Mat::operator = (const Mat& m) { if( this != &m ) { if( m.refcount ) CV_XADD(m.refcount, 1); release(); flags = m.flags; if( dims <= 2 && m.dims <= 2 ) { dims = m.dims; rows = m.rows; cols = m.cols; step[0] = m.step[0]; step[1] = m.step[1]; } else copySize(m); data = m.data; datastart = m.datastart; dataend = m.dataend; datalimit = m.datalimit; refcount = m.refcount; allocator = m.allocator; } return *this; }该重载运算符只是将各个数据指针的地址进行了赋值,并没有复制数据的操作。
那么如果我们需要复制Mat对象,应该如何操作呢?
(1)Mat::copyTo函数
Mat a; Mat b; a.copyTo(b);
Mat a; Mat b = a.clone();
我们查看源代码:
inline Mat Mat::clone() const { Mat m; copyTo(m); return m; }
OpenCV(C++接口)学习笔记4-Mat::operator = 的陷阱,布布扣,bubuko.com
OpenCV(C++接口)学习笔记4-Mat::operator = 的陷阱
标签:des style color io strong 数据 for ar
原文地址:http://blog.csdn.net/szlcw1/article/details/38519883