Ask Your Question

Does Mat::create() reallocate when new size is smaller?

asked 2013-03-08 13:36:04 -0500

Adi gravatar image

updated 2013-03-12 02:48:16 -0500

Say I have an allocated cv::Mat img of size 100x100. Now I call img.create(50,50,img.type()).
Since the type is the same, there is, obviously, enough memory already allocated, and all that's needed is to set a proper "ROI" on the existing Mat.

Does create() actually do this, or does it release and re-allocate since the size is different (50x50 vs. 100x100)?

An std::vector<> uses the same stratgy. You can resize() it to 0, but it will still keep the internal buffer available so that growing again later, e.g. with push_back() does not reallocate. The same thing could be done with ROIs.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-03-08 13:51:13 -0500

awknaust gravatar image

updated 2013-03-08 13:52:09 -0500

It will reallocate unless the size is identical. It would be hard to keep track of the data otherwise.

edit flag offensive delete link more


Why? There is a built in facility for ROIs/sub-matrices?

Adi gravatar imageAdi ( 2013-03-08 13:55:44 -0500 )edit

Yes so if you wanted to create a sub-matrix you would use that, not create(). By saying create() you are telling it that you no longer care about the rest of the data, so why would OpenCV keep it around? When you have a ROI, it doesn't rule out that you might still be interested in the original.

awknaust gravatar imageawknaust ( 2013-03-10 13:38:57 -0500 )edit

See my edit about `std::vector` above.

Adi gravatar imageAdi ( 2013-03-12 02:48:59 -0500 )edit

Question Tools


Asked: 2013-03-08 13:36:04 -0500

Seen: 527 times

Last updated: Mar 12 '13