# 12bit to 8 bit and Windowing

19 views (last 30 days)
David Großmann on 19 Jan 2020
Commented: David Großmann on 21 Jan 2020 at 19:27
Hallo,
I try to dicomread a 12bit-pic, change it to 8bit and windows it.
With
im8 = uint8(im-1);
imshow(im8, [LOW,HIGH]);
Everything works fine. Matlab cuts all the values that aren't relevant and turns them into white or black.
But is there a function that does the same without directly displaying the picture?
When I use the following pictures do not look the same(it's darker):
[nrows ncols] = size(im);
for c = 1:ncols
for r = 1:nrows
if (im(r,c) < LOW)
im2(r,c) = 0;
elseif (im(r,c) > HIGH)
im2(r,c) = 255;
else
im2(r,c) = im(r,c);
end
end
end
im8er = uint8(im2);
imshow(im8er);
Shall I cast to uint8 before or after cutting the values?
Thank You!

#### 1 Comment

Guillaume on 20 Jan 2020
If im is 12-bit (integer I presume), this is a very odd thing to do:
im8 = uint8(im-1);
If the 12 bit is signed (range [-2048, 2047]), this rescale im to the range [-2049, 2046] (assuming the 12-bits are stored in a 16-bit integer since matlab doesn't have 12 bit type) which is then clipped to [0 255] by the conversion to uint8. So it maps all values between -2048 and 1 included to 0, maps 1-256 to 0-255 and maps 256+ to 255.
If the 12 bit is unsigned (range [0 4095]), this rescale im to [0 4094] which is then clipped to [0 255]. So it maps 0 and 1 to 0, 2-256 to 1-255 and 256+ to 255.

Walter Roberson on 19 Jan 2020
In older MATLAB see mat2gray, which has a deceptive function name and is not restricted to grayscale like the name suggests.

David Großmann on 20 Jan 2020
Thank you, but sadly I'm not allowed to use these. So have you got a different idea?
Walter Roberson on 20 Jan 2020
scaled = (double(im) - LOW) ./ (HIGH - LOW);
im8er = im2uint8(scaled);
David Großmann on 21 Jan 2020 at 19:27
Wow thanks, this looks perfectly fine!