使用opencv将16位深度图转灰度图,默认深度图每个像素以uint16_t来保存。
#include <string>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
void depth_img_save(uint16_t* raw_data)
{
int rows = 480;
int cols = 640;
Mat depth_img = Mat(rows,cols,CV_8UC1);
int max_depth = 10000; //默认最大测距为10000mm
for (int i = 0; i < rows; i++) //480 默认先行后列保存
{
for (int j = 0; j < cols; j++) //640
{
//每个距离归一化后*255,即映射到0-255范围
uchar pixel = raw_data[i*cols+j]*255/max_depth;
depth_img.at<uchar>(i,j) = pixel;
}
}
imshow("depth_img",depth_img);
imwrite("depth_img.jpg",depth_img);
waitKey();
}