两种读取方式
1、指针方式读取
float d = (float)depth.ptr<unsigned short>(n)[m] /1000;
ptr函数会返回指向该图像第m行数据的头指针,然后在改行加上位移n,表示取第m行,第n列位置处的深度值;
2、动态地址访问方式
float d = (float)depth.at<unsigned short>(n,m) / 1000;
这种方式就很直观,但是耗时较长。
编程中遇到的问题
读取到的数据值为0,查阅资料发现是数据类型写错导致的,如下写法:
float d = (unsigned short)depth.ptr<float>(n)[m] /1000;
编程中一定要弄清楚数据格式再进行操作。
编码格式对应关系
Mat每个格子内的数据格式-----------Mat定义
Mat_<uchar>---------CV_8U
Mat<char>-----------CV_8S
Nat_<short>---------CV_16S
Mat_<ushort>--------CV_16U
Mat_<int>-----------CV_32S
Mat_<float>----------CV_32F
Mat_<double>--------CV_64F
img1.at<vec3b>(i,j)[0]= 3.2f; // B 通道
img1.at<vec3b>(i,j)[1]= 3.2f; // G 通道
img1.at<vec3b>(i,j)[2]= 3.2f; // R 通道