Convert 32 bit image to 8 bit image

43 views (last 30 days)
windmill
windmill on 16 Dec 2020
Edited: DGM on 11 May 2024
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?

Answers (4)

James Tursa
James Tursa on 16 Dec 2020
Edited: DGM on 11 May 2024
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 on 16 Dec 2020
Edited: Walter Roberson on 16 Dec 2020
IMG8 = im2uint8(IMG);
  1 Comment
DGM
DGM on 11 May 2024
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'}, ...

Sign in to comment.


Image Analyst
Image Analyst on 21 Dec 2020
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 on 11 May 2024
Edited: DGM on 11 May 2024
@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.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!