Ask Your Question

Create a memory continuous cv::Mat, any api could do that?

asked 2013-10-20 02:16:36 -0500

stereomatching gravatar image

updated 2013-10-20 04:31:03 -0500

berak gravatar image
cv::Mat dst(src.size(), src.type());

I want to make sure the memory of the dst is continuous(without padding), how could I do that?

The most easiest answer is call the reshape after creation.


But how could I make sure it is continuous when construct?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-10-20 04:27:43 -0500

berak gravatar image

updated 2013-10-20 04:28:45 -0500

there are 3 cases, where you'd get non-continuous Mat's.

  • it's a roi
  • bmp images and the like sometimes come padded
  • you made a mat from a pixel pointer ( i.e some non-opencv capture device ) and that might be padded, too

since you have to reorder the memory in all those cases, checking for continuity and a clone() acll will solve it.

if ( ! mat.isContinuous() )
    mat = mat.clone();
edit flag offensive delete link more


May I say the cv::Mat will allocate continuous memory for me if I construct it like this "cv::Mat dst(src.size(), src.type());"?

stereomatching gravatar imagestereomatching ( 2013-10-20 05:35:38 -0500 )edit

yes, you can rely on that

berak gravatar imageberak ( 2013-10-20 05:39:26 -0500 )edit

answered 2013-10-20 03:27:17 -0500

Michael Burdinov gravatar image

First, reshape() does not performs any copy of memory. So if the memory wasn't continuous it will stay this way.

Second, Mat unlike IplImage allocates continuous memory during its construction unless you ask otherwise. So don't worry about that.

Third, if still do not trust Mat to allocate continuous memory for you, you can allocate it yourself and pass it Mat constructor. Don't forget to release it afterwards. I think this is really an overkill but if you insist....

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2013-10-20 02:16:36 -0500

Seen: 6,976 times

Last updated: Oct 20 '13