OpenCV C++: how access pixel value CV_32F through uchar data pointer

前端 未结 2 1450
遇见更好的自我
遇见更好的自我 2020-12-09 06:17

Briefly, I would like to know if it is possible to directly access pixel value of a CV_32F Mat, through Mat member \"uchar* data\".

I can do it with no problem if Ma

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 06:56

    You can use ptr method which returns pointer to matrix row:

    for (int y = 0; y < mat.rows; ++y)
    {
        float* row_ptr = mat.ptr(y);
        for (int x = 0; x < mat.cols; ++x)
        {
            float val = row_ptr[x];
        }
    }
    

    You can also cast data pointer to float and use elem_step instead of step if matrix is continous:

    float* ptr = (float*) mat.data;
    size_t elem_step = mat.step / sizeof(float);
    float val = ptr[i * elem_step + j];
    

提交回复
热议问题