将 CV_32FC1 转换为 CV_16UC1 [英] Converting CV_32FC1 to CV_16UC1
本文介绍了将 CV_32FC1 转换为 CV_16UC1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将从模拟深度相机获得的 float
图像转换为 CV_16UC1
.相机以 CV_32FC1
格式发布深度.我尝试了很多方法,但结果都不合理.
I am trying to convert a float
image that I get from a simulated depth camera to CV_16UC1
. The camera publishes the depth in CV_32FC1
format. I tried many ways but the result was not reasonable.
cv::Mat depth_cv(512, 512, CV_32FC1, depth);
cv::Mat depth_converted;
depth_cv.convertTo(depth_converted,CV_16UC1);
结果是黑色图像.如果我使用比例因子,图像将是白色的.
The result is a black image. If I use a scale factor, the image will be white.
我也尝试过这样做:
float depthValueF [512*512];
for (int i=0;i<resolution[1];i++){ // go through the rows (y)
for (int j=0;j<resolution[0];j++){ // go through the columns (x)
depthValueOfPixel=depth[i*resolution[0]+j]; // this is location j/i, i.e. x/y
depthValueF[i*resolution[0]+j] = (depthValueOfPixel) * (65535.0f);
}
}
它也没有成功.
推荐答案
尝试使用 cv::normalize
相反,它不仅会将图像转换为正确的数据类型,而且还会在幕后为您正确地进行缩放.
Try using cv::normalize
instead, which will not only convert the image into the proper data type, but it will properly do the scaling for you under the hood.
因此:
cv::Mat depth_cv(512, 512, CV_32FC1, depth);
cv::Mat depth_converted;
cv::normalize(depth_cv, depth_converted, 0, 65535, NORM_MINMAX, CV_16UC1);
这篇关于将 CV_32FC1 转换为 CV_16UC1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文