Based on my humble understanding, OpenCV's Mat handles the memory management efficiently; so copying Mats does not mean they are "hard/physically" copied; they just refer to the original Mat.

However, for mats that has been pushed into a larger Mat using push_back, is it safe to clear them assuming that they were hard copied, not using same technique of copying like in x=y?

In the following code, does bigx still has x's contents even after releasing the latter?

Mat x, bigx;
bigx.push_back(x);
x.release();

thank you :)

有帮助吗?

解决方案

As far as I know Mat::pushback() will create a separate copy of source on each pushback. So you can release your source after pushback.

See an example below,

   Mat src=imread("src.jpg",1);
   int rowSize=src.rows;
   Mat A;
   A.push_back(src.reshape(0,1));
   src.release();

   Mat B;
   B = A.row(0).clone();
   imshow("src",B.reshape(0,rowSize));
   waitKey(); 

其他提示

Yes, push_back calls the copy constructor to create a fresh clone element of same type keeping the original (source) at the discretion.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top