从原始缓冲区数据创建 Mat 图像

Mat Image creation from raw buffer data

我有图像的 float x、float y、float z 值。我想通过复制 z 值来构造一个 16 位 png 深度图像。结果我得到的图像有一些无效点。下面是我的代码。

 uint16_t* depthValues = new uint16_t[size];
auto sampleVector(DepthPoints);
for (unsigned int i = 0; i < sampleVector.size(); i++)
    {
        depthValues[i] = (sampleVector.at(i).z) * 65536;
    }
  Mat newDepthImage = cv::Mat(var.height, var.width, CV_16UC1,depthValues);
imwrite(Location, CImage);

有人能告诉我,我是否可以将浮点值复制到一个无符号字符数组中来创建图像? 这就是我的图像有无效点的原因吗?

auto sampleVector(DepthPoints);
const int size = sampleVector.size();
float* depthValues = new float[size];
for (unsigned int i = 0; i < sampleVector.size(); i++)
    {
      depthValues[i] = (sampleVector.at(i).z);
    }
Mat depthImageOne, depthImageTwo;
Mat depthImageNew = cv::Mat(var.height, var.width, CV_32FC1,depthValues);
normalize(newDepthImageNew, depthImageOne, 1, 0, NORM_MINMAX, CV_32FC1);
depthImageOne.convertTo(depthImageTwo, CV_16UC1, 65536.0,0.0);
imwrite("path", depthImageTwo);

规范化可能会导致深度信息丢失。我已经使用标准化来可视化图像。为了保留深度信息,我使用了以下代码。

Mat depthImageNew = cv::Mat(var.height, var.width, CV_32FC1,depthValues);
depthImageOne.convertTo(depthImageTwo, CV_16UC1, 1000.0,0.0);