opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
78.49k stars 55.76k forks source link

confusion about Mat::data, datastart, dataend, datalimit, step #8304

Closed darkdragon-001 closed 7 years ago

darkdragon-001 commented 7 years ago

Can somebody explain me the exact purpose of these member fields?

Since these members are neither well documented via Doxygen (see member definitions ) nor seems their purpose clear to other developers (see code comments).

alalek commented 7 years ago

Direct using of fields in well written code should be avoided, please use methods instead: .ptr()/.at() with .step(), .elemSize() methods. They are more safe and provide some useful checks in debug mode.

darkdragon-001 commented 7 years ago

For my time critical application, I am using direct pointers for speed reasons.

Is my following interpretation correct? mat

alalek commented 7 years ago

datastart <= data <= dataend <= datalimit. On this picture your should swap data <-> datastart. datalimit is almost not used (and it may be located after the current "datalimit" pointer). dataend should be placed on datalimit location. There is no field which points "on the end" of green region.

step[1] (which is step[dims-1]) is always equal to sizeof(matrix element) (CV_8U => 1, CV_32FC3 => 12).

See locateROI() code.

BTW, There is good simple example of efficient code.

darkdragon-001 commented 7 years ago

So datalimit points to the end off allocated storage?

Does this picture now represent reality better? mat

alalek commented 7 years ago

Based on this code, dataend should be less or equal than datalimit.

On your picture there is missing "alignment area" on the right side (for example), that doesn't hold any matrix elements, but it aligns rows starting addresses. BTW, In general step[0] may be not divisible on element size.

dataend is located after last valid image byte. datalimit probably points to the end of allocated buffer (after "alignment area").

I assume that green is Image ROI (region of interest), white cells are Image data (these values can be used too, for example in image filters for border values (if there is no BORDER_ISOLATE flag).

darkdragon-001 commented 7 years ago

In order to avoid confusion (is it allocated space or not?), I guess one should clearly define datalimit and document it directly in the code.

Is anything else missing in my image?

mat

Legend

white background: alloated data
black border: matrix elements
green background: ROI

Data Types

pointers: datastart, data, dataend, datalimit
size in bytes: step
number of elements: rows, cols

vpisarev commented 7 years ago

ok, it can be closed, as it still can be referenced, even after being closed

zengjie617789 commented 1 month ago

what about if the image has multi channel?