Ask Your Question
0

isContinuous() behaviour for large images in OpenCV

asked 2018-07-19 05:14:23 -0600

hovnatan gravatar image

Hi, I have the following in OpenCV 3.4.2

cv::Mat3b a(cv::Size(30511, 26060));
cout << a.isContinuous() << endl;

and the result is 0 (false), which I think is not correct looking at the step size in a.step[0]. Is this a bug or some quirk with large arrays? Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
-1

answered 2018-07-19 05:51:06 -0600

kbarni gravatar image

First, the step defines the data layout of the array a. It shouldn't be used for element access. a.step[0] gives you the number of bytes needed to store a line (first dimension); a.step[1] for a pixel (second dimension), a.step[2] for the third dimension (channel).

Images don't have to be continuous. Large images can be split up to better fit in the memory. Lines are always stored in a continuous memory block (step[0] bytes).

As a general rule, it's better to consider matrices as non-continuous and use the ptr function to get the address of a line. To access an element (x,y), you should use (considering that you have an element size of 1):

a.ptr(y)+x

instead of

a.data()+y*a.cols()+x
edit flag offensive delete link more

Comments

I read that cv::Mat::clone() will always give continuous image, but it is not the case here. So is that statement incorrect?

hovnatan gravatar imagehovnatan ( 2018-07-19 06:00:52 -0600 )edit

From OpenCV documentation on cv::Mat::clone() "The method creates a full copy of the array. The original step[] is not taken into account. So, the array copy is a continuous array occupying total()*elemSize() bytes.". I think this is a bug.

hovnatan gravatar imagehovnatan ( 2018-07-19 06:03:46 -0600 )edit
1

And also I think a.step[2] is not for the channel. It is for 3 dimensional images. You can try accessing a.step[2] for a cv::Mat3b, it crashes.

hovnatan gravatar imagehovnatan ( 2018-07-19 06:05:35 -0600 )edit

Sorry, my bad. step[0] is the line size (2nd dimension), step[1] is the 3rd dimension - i.e. the number of channels or the number of planes.

OTOH I checked your code, and I always get 1 (true) for the continuity - even on 32 bit 3 channel images. It takes about 2GB of memory, but it's always continuous on my computer. I use OpenCV 3.4.1 on Ubuntu 64bit.

kbarni gravatar imagekbarni ( 2018-07-19 08:56:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-19 05:14:23 -0600

Seen: 503 times

Last updated: Jul 19 '18