gpt4 book ai didi

c++ - OpenCV:如何可视化深度图像

转载 作者:IT老高 更新时间:2023-10-28 21:38:44 33 4
gpt4 key购买 nike

我正在使用一个数据集,其中包含图像,其中每个像素都是一个 16 位无符号整数,以毫米为单位存储该像素的深度值。我试图通过执行以下操作将其可视化为灰度深度图像:

cv::Mat depthImage; 
depthImage = cv::imread("coffee_mug_1_1_1_depthcrop.png", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR ); // Read the file
depthImage.convertTo(depthImage, CV_32F); // convert the image data to float type
namedWindow("window");
float max = 0;
for(int i = 0; i < depthImage.rows; i++){
for(int j = 0; j < depthImage.cols; j++){
if(depthImage.at<float>(i,j) > max){
max = depthImage.at<float>(i,j);
}
}
}
cout << max << endl;


float divisor = max / 255.0;
cout << divisor << endl;
for(int i = 0; i < depthImage.rows; i++){
for(int j = 0; j < depthImage.cols; j++){
cout << depthImage.at<float>(i,j) << ", ";
max = depthImage.at<float>(i,j) /= divisor;
cout << depthImage.at<float>(i,j) << endl;
}
}


imshow("window", depthImage);
waitKey(0);

但是,它只显示两种颜色,这是因为所有值都非常接近,即在 150-175 的范围内 + 显示为黑色的小值(见下文)。

rgb image greyscale image

有没有办法对这些数据进行归一化,使其显示各种灰度级以突出这些小的深度差异?

最佳答案

根据documentation ,函数 imshow 可用于多种图像类型。它支持 16 位无符号图像,因此您可以使用显示图像

cv::Mat map = cv::imread("image", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
cv::imshow("window", map);

在这种情况下,图像的取值范围从 [0, 255*256] 范围映射到 [0, 255] 范围。

如果您的图像仅包含此范围的较低部分的值,您将观察到模糊的图像。如果你想使用完整的显示范围(从黑色到白色),你应该调整图像以覆盖预期的动态范围,一种方法是

double min;
double max;
cv::minMaxIdx(map, &min, &max);
cv::Mat adjMap;
cv::convertScaleAbs(map, adjMap, 255 / max);
cv::imshow("Out", adjMap);

关于c++ - OpenCV:如何可视化深度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13840013/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com