Does Mat::push_back(x) copy x elements?

风格不统一 提交于 2019-12-24 14:14:05

问题


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 :)


回答1:


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(); 



回答2:


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



来源:https://stackoverflow.com/questions/23008597/does-matpush-backx-copy-x-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!