简体   繁体   English

将Matlab视差图像从单精度转换为uint8

[英]Convert Matlab disparity image from single precision to uint8

I need to save the disparity image to my disk. 我需要将视差图像保存到磁盘上。 The data type of the disparity image is single precision and the disparity range is [0 128]. 视差图像的数据类型是单精度,并且视差范围是[0 128]。 While using imwrite(disparityMap,file_name) , the saved image appears to be binary. 使用imwrite(disparityMap,file_name) ,保存的图像似乎是二进制的。

When you use imwrite with floating point precision, matlab consider that your data is in the range [0 1]. 当您以浮点精度使用imwrite时,matlab会认为您的数据在[0 1]范围内。 So any value above 1 will be considered as 1. That is why you have a black and white image. 因此,任何大于1的值都将被视为1。这就是为什么您拥有黑白图像的原因。

From matlab doc : matlab doc

If A is a grayscale or RGB color image of data type double or single, then imwrite assumes the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values. 如果A是数据类型为double或single的灰度或RGB彩色图像,则imwrite假定动态范围为[0,1],并在将数据作为8位值写入文件之前自动将其缩放255。

Then, you have two solution. 然后,您有两种解决方案。 I'm considering that 128 is the maximum in your data, and that you want a colormap that goes from black to white. 我正在考虑您的数据中最大为128,并且您想要一个从黑色到白色的色图。 I will 我会

First solution, normalize your data so that matlab do the right conversion: 第一个解决方案,规范化数据,以便matlab进行正确的转换:

% Normalize your data between 0 and 1
disparityMap = disparityMap/128;

% write the image
imwrite(disparityMap,file_name)

Second solution, do the conversion yourself and write the image directly as uint8: 第二种解决方案,自己进行转换并将图像直接写为uint8:

% Normalize your data between 0 and 255 and convert to uint8
disparityMapU8 = uint8(disparityMap*255/128);

% write the image as uint8
imwrite(disparityMapU8,file_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM