Convert 32 bit image to 8 bit image

40 ビュー (過去 30 日間)
windmill
windmill 2020 年 12 月 16 日
編集済み: DGM 2024 年 5 月 11 日
Hey,
I have an .tif grayscale image with a bit depth of 32. I want to convert it from 32 bit to 8 bit. I tried using mat2gray, but every time I tried displaying the image after converting it, it didn't look at all like it before. It was all black with only a few barely visible brighter spots. Does anyone know ho to properly perform the transformation?

回答 (4 件)

James Tursa
James Tursa 2020 年 12 月 16 日
編集済み: DGM 2024 年 5 月 11 日
I'm assuming you just need to scale things. E.g.,
X = your image (as a unit32?)
Y = uint8( double(X) * ((2^8-1)/(2^32-1)) );
There may be a MATLAB function for this, but I am not familiar with the image manipulation functions.

Walter Roberson
Walter Roberson 2020 年 12 月 16 日
編集済み: Walter Roberson 2020 年 12 月 16 日
IMG8 = im2uint8(IMG);
  1 件のコメント
DGM
DGM 2024 年 5 月 11 日
IPT im2uint8() does not support anything other than the six standard IPT image classes.
inpict = imread('peppers_uint32.tiff.fakeextension.txt'); % RGB, uint32
inpict = im2uint8(inpict);
Error using im2uint8
Expected input number 1, Image, to be one of these types:

double, logical, uint8, uint16, single, int16

Instead its type was uint32.

Error in im2uint8 (line 40)
validateattributes(img,{'double','logical','uint8','uint16','single','int16'}, ...

サインインしてコメントする。


Image Analyst
Image Analyst 2020 年 12 月 21 日
The mat2gray() approach would be this:
To turn it into a double image:
% Scale so min -> 0, and max -> 1.0
dblImage = mat2gray(uint32Image);
imshow(dblImage);
To turn it into an 8 bit uint8 image with the lowest gray level a proprotionally scaled value
% Divide by max so min = something, and max -> 255. Something is not 0 unless the min was 0.
uint8Image = uint8(255 * double(uint32Image) / max(uint32Image(:)));
imshow(uint8Image);
To turn it into an 8 bit uint8 image with the lowest gray level at 0
% Scale so min -> 0, and max -> 255
uint8Image = uint8(255 * mat2gray(uint32Image));
imshow(uint8Image);

DGM
DGM 2024 年 5 月 11 日
編集済み: DGM 2024 年 5 月 11 日
@James Tursa's answer is correct, but there are tools that can do the job -- just not within IPT.
MIMT imcast() is to IPT im2double() and im2uint8() what cast() is to double() and uint8(). The fact that it's parametric is normally the appeal, but what's important here is that it actually supports a broader range of numeric classes than IPT tools do.
inpict = imread('my32bimage.tiff'); % uint32 or int32
inpict = imcast(inpict,'uint8'); % uint8
Easy as that. No math to remember. No conditional wrappers to write. Scale and contrast are preserved. Properly-scaled image in, properly-scaled image out.

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by