gpt4 book ai didi

c++ - OpenCV(Cpp接口(interface))——内存管理

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:51 26 4
gpt4 key购买 nike

我发现 OpenCV 内存管理相当困惑。我在这里阅读文档 http://opencv.itseez.com/modules/core/doc/intro.html#automatic-memory-management ,但我真的不认为它提供了足够的信息来完全理解它。

例如考虑以下片段

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

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

关于c++ - OpenCV(Cpp接口(interface))——内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7918332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com