Access value with a pointer in Mat [closed]

asked 2018-12-30 03:22:34 -0600

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.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by LBerger
close date 2019-01-02 07:35:01.620423

Comments

For one pixel you can use pointer access :

double *x=(double *)test.data
*(x+ 2 * test.step[1])=*test.data + 10;

it's better llike this

double *ptr1=test.ptr<double>(2)
double *ptr2=test.ptr<double>(0)
*ptr1 = *ptr2 +10
LBerger gravatar imageLBerger ( 2018-12-30 03:51:27 -0600 )edit

what are you trying to achieve here ? what is the purpose of your program ?

(in general, you probably should avoid raw pointers, and use higher level opencv functions instead)

berak gravatar imageberak ( 2018-12-30 06:37:31 -0600 )edit
2

Thank you very much. I'm a beginner in OpenCV. I just want to test the pointer data, I will follow your advice to use ptr pointer later.

happy__ gravatar imagehappy__ ( 2019-01-02 07:05:41 -0600 )edit
LBerger gravatar imageLBerger ( 2019-01-02 07:34:44 -0600 )edit