C++ OpenCV(Cpp接口)-内存管理
我发现OpenCV内存管理非常混乱。我阅读了这里的文档,但我真的认为它没有提供足够的信息来完全理解它C++ OpenCV(Cpp接口)-内存管理,c++,opencv,C++,Opencv,我发现OpenCV内存管理非常混乱。我阅读了这里的文档,但我真的认为它没有提供足够的信息来完全理解它 例如考虑下面的代码段 Mat_<float> a,b,c; a = b; // The header of b is copied into a and they share the data b = c; // Now b refers to c and a != b b = a + 1; // b still shares data with c and s.t. b = c;
例如考虑下面的代码段
Mat_<float> a,b,c;
a = b; // The header of b is copied into a and they share the data
b = c; // Now b refers to c and a != b
b = a + 1; // b still shares data with c and s.t. b = c;
材料a、b、c;
a=b;//b的标题被复制到a中,它们共享数据
b=c;//现在b指的是c和a!=B
b=a+1;//b仍然与c共享数据,s.t.b=c;
这有什么意义吗?有人可以解释它背后的想法吗?您需要将内存分配与声明矩阵a、b和c分开
cv::Mat b(10, 10, CV8U_C1); //this allocates 10 rows and 10 columns of 8 bit data to matrix b
cv::Mat a; //This defines a matrix with an empty header. You *cannot* yet assign data to it - trying to do so will give a segmentation fault
a = b; //matrix a is now equal to matrix b. The underlying data (a pointer to 10 x 10 uints) is shared by both so it is a shallow copy (and thus very efficient). However modifying the data in martix a will now modify the data in matrix b
cv::Mat c(10, 10, CV8U_C1);
b = c; //This will change matrix b to point to the newly allocated data in matrix c. Matrix a now has the sole access to its data as matrix b no longer shares it. Matrix b and c share the same data;
b = a + 1 //This statement makes no sense. Even if it is valid you should never use it - it is completely unclear what it does
您需要分别分配内存和声明矩阵a、b和c
cv::Mat b(10, 10, CV8U_C1); //this allocates 10 rows and 10 columns of 8 bit data to matrix b
cv::Mat a; //This defines a matrix with an empty header. You *cannot* yet assign data to it - trying to do so will give a segmentation fault
a = b; //matrix a is now equal to matrix b. The underlying data (a pointer to 10 x 10 uints) is shared by both so it is a shallow copy (and thus very efficient). However modifying the data in martix a will now modify the data in matrix b
cv::Mat c(10, 10, CV8U_C1);
b = c; //This will change matrix b to point to the newly allocated data in matrix c. Matrix a now has the sole access to its data as matrix b no longer shares it. Matrix b and c share the same data;
b = a + 1 //This statement makes no sense. Even if it is valid you should never use it - it is completely unclear what it does
为了对这个问题有一个大致的了解,你必须阅读一些关于智能指针的理论
OpenCV中的许多对象(包括Mat)都实现为智能指针 要想大致了解这个问题,你必须阅读一些关于智能指针的理论
OpenCV中的许多对象(包括Mat)都实现为智能指针 为什么b=a+1毫无意义?几乎每一个linalg库都会重载操作符,所以我想这很常见。它只是一个元素的加法。我的问题是:为什么b=a+1;//b仍然与c共享数据,s.t.b=c;b不应该再和c共享数据了,对吗?它应该只指向新分配的矩阵a+1;为什么b=a+1毫无意义?几乎每一个linalg库都会重载操作符,所以我想这很常见。它只是一个元素的加法。我的问题是:为什么b=a+1;//b仍然与c共享数据,s.t.b=c;b不应该再和c共享数据了,对吗?它应该只指向新分配的矩阵a+1;我知道,但是考虑呼叫B= A+ 1;首先(a+1)生成一个新的矩阵Mat运算符+(Mat,标量),然后将这个新矩阵分配给b。因此,b不应该指向新的(a+1)矩阵,而不是将数据复制到其指向的内存中吗?Mat对象的设计使其在新指定对象的大小与以前相同时不会重新创建内存。所以结果保存在旧的数据空间中,实际上是矩阵c和b共享的空间。如果你给b分配一个更大的矩阵(b.create(10,10,CV_8UC1)),旧的数据指针将只被c使用,b将有自己的空间。我明白了。听起来很容易出错,他们应该改变它。谢谢,这是OpenCV最好的创意之一。易于使用,同时速度极快。在进行实时图像处理时,alloc/dealloc的成本可能非常高。您可以编写非常干净、安全的代码。你只需要习惯它的风格。我的意思是,共享指针很好,但要么总是共享的,要么不是。b=Mat(M,N)不应该根据M,N有不同的行为(如您所说,如果M*N=size(b),结果将存储在旧空间中,否则将分配新空间,并且对b的所有引用都将被破坏)。当程序变得难以预测运行时变化的参数时,我知道,但是考虑调用B= A+ 1;首先(a+1)生成一个新的矩阵Mat运算符+(Mat,标量),然后将这个新矩阵分配给b。因此,b不应该指向新的(a+1)矩阵,而不是将数据复制到其指向的内存中吗?Mat对象的设计使其在新指定对象的大小与以前相同时不会重新创建内存。所以结果保存在旧的数据空间中,实际上是矩阵c和b共享的空间。如果你给b分配一个更大的矩阵(b.create(10,10,CV_8UC1)),旧的数据指针将只被c使用,b将有自己的空间。我明白了。听起来很容易出错,他们应该改变它。谢谢,这是OpenCV最好的创意之一。易于使用,同时速度极快。在进行实时图像处理时,alloc/dealloc的成本可能非常高。您可以编写非常干净、安全的代码。你只需要习惯它的风格。我的意思是,共享指针很好,但要么总是共享的,要么不是。b=Mat(M,N)不应该根据M,N有不同的行为(如您所说,如果M*N=size(b),结果将存储在旧空间中,否则将分配新空间,并且对b的所有引用都将被破坏)。当程序开始运行时,很难预测运行时不断变化的参数。。。