I've seen a lot of OpenCV code which accesses the data member of a cv::Mat
directly. cv::Mat
stores the pointer to the data in a unsigned char* data
member. Access to the data member looks like:
cv::Mat matUC(3,3,CV_8U)
int rowIdx = 1;
int colIdx = 1;
unsigned char val = matUC.data[ rowIdx * matUC.cols + colIdx]
I'm wondering if this works for a cv::Mat
with pixel type other than unsigned char
.
cv::Mat matF(3,3,CV_32F)
int rowIdx = 1;
int colIdx = 1;
float val = matF.data[ rowIdx * matF.cols + colIdx];
My understanding would be that a typecast is needed to access the elements correctly. Something like:
float val = ((float*)matF.data)[ rowIdx * matF.cols + colIdx];
I've seen a lot of code which doesn't use a typecast. So my question is: Is the typecast mandatory to access the correct element?