Ask Your Question

Revision history [back]

Access value with a pointer in Mat

A Mat element is stored in Mat data pointer described in OpenCV document, and the address is mat.data + i0 * mat.step[0] + i1 * mat.step[1] +... + (id-1) * mat.step[id - 1] . I want to access and re-assign an element with data pointer like this:

cv::Mat test = (cv::Mat_<uchar>(3,3) << 1,2,3,4,5,6,7,8,9);
*(test.data + 2 * test.step[1]) = *test.data + 10;

if the element type of Mat is uchar, the code above works well, and the third element is re-assigned as 11, but if the type is not uchar, such as double, the code above does not work, I'm confused about this, please help me.