Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In which case Mat data is in continuous memory block and which case is not?

I know that OpenCV has the method isContinuous() to check. However, what I want to know is in which cases we know for sure that Mat data is in continuous memory block. So far, based on what I've been searching around, it seems that in these cases Mat data would be in continuous memory block: - Read an image using imread() - Create a new Mat using Mat() constructor - Using create() method - Using initializers like zeros(), ones(), etc.

In these cases Mat data would NOT be in continuos memory block: - Take ROI from an existing Mat - Take some columns from an existing Mat

Is it correct? Is there any other cases? Or can you provide a more generalized look? Please don't tell me that I should not worry about memory management as OpenCV takes care of it internally. It's much easier if we know for sure which cases we have continuous memory block.

Thanks a lot.

PS: The reason I come up with this question is from the accepted answer in SO: http://stackoverflow.com/questions/4170745/serializing-opencv-mat-vec3f/36207004#36207004

in this part:

for (size_t dc = 0; dc < dataSize; ++dc) {
          ar & m.data[dc];
}

I think he assumes the Mat data is in continuous memory block, which I know is not always the case.

In which case Mat data is in continuous memory block and which case is not?

I know that OpenCV has the method isContinuous() to check. However, what I want to know is in which cases we know for sure that Mat data is in continuous memory block. So far, based on what I've been searching around, it seems that in these cases Mat data would be in continuous memory block: - block:

  • Read an image using imread() - imread()
  • Create a new Mat using Mat() constructor - constructor
  • Using create() method - method
  • Using initializers like zeros(), ones(), etc.

In these cases Mat data would NOT be in continuos memory block: - block:

  • Take ROI from an existing Mat - Mat
  • Take some columns from an existing Mat

Is it correct? Is there any other cases? Or can you provide a more generalized look? Please don't tell me that I should not worry about memory management as OpenCV takes care of it internally. It's much easier if we know for sure which cases we have continuous memory block.

Thanks a lot.

PS: The reason I come up with this question is from the accepted answer in SO: http://stackoverflow.com/questions/4170745/serializing-opencv-mat-vec3f/36207004#36207004

in this part:

for (size_t dc = 0; dc < dataSize; ++dc) {
          ar & m.data[dc];
}

I think he assumes the Mat data is in continuous memory block, which I know is not always the case.