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

Convert Matlab disparity image from single precision to uint8

我需要将视差图像保存到我的磁盘中。视差图的数据类型为单精度,视差范围为[0128]。 使用 imwrite(disparityMap,file_name) 时,保存的图像似乎是二进制的。

当您使用浮点精度的 imwrite 时,matlab 认为您的数据在 [0 1] 范围内。所以任何大于 1 的值都将被视为 1。这就是为什么你有一张黑白图像。

来自 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.

那么,你有两个解决方案。我正在考虑 128 是您数据中的最大值,并且您想要一个从黑色到白色的颜色图。我会

第一个解决方案,将数据标准化,以便 matlab 进行正确的转换:

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

% write the image
imwrite(disparityMap,file_name)

方案二,自己转换,把图片直接写成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)