MATLAB: 12bit to 8 bit and Windowing

12bit8bitwindowing

Hallo,
I try to dicomread a 12bit-pic, change it to 8bit and windows it.
With
im = dicomread("CTA_Head.dcm");
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):
im = dicomread("CTA_Head.dcm");
[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!

Best Answer

In older MATLAB see mat2gray, which has a deceptive function name and is not restricted to grayscale like the name suggests.
In newer MATLAB rescale()
Related Question