필터 지우기
필터 지우기

Convert 32 bit image to 8 bit image

조회 수: 37 (최근 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.

Community Treasure Hunt

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

Start Hunting!

Translated by