Convert the intensity of image into a specific intensity range

Hello everybody,
I have 100 3D medical images that have different intensity ranges. most of them have a maximum intensity of 255( and their type is single) but that of some of them is about 2000-3000( uint16). How can I convert the intensity of all images into [0,255]?
I tried this: 255*( (Img - min(Img(:)))./max(img(:))) but it didn't give the expected result. please help me with this problem.
Thank you.

1 Comment

Since this is likely about CT images, you need to decide what window level and width makes sense for your application. Looking for lung nodules requires a very different window than determining emphysema severity.

Sign in to comment.

 Accepted Answer

You can use mat2gray:
Img2 = uint8(255 * mat2gray(Img));
or you can use rescale
Img2 = rescale(Img, 0, 255);
rescale gives a floating point output. Cast to uint8 if you want uint8. Pick whatever min and max you want. We can give a better answer if you tell us why the code you used did not give the expected answer.

5 Comments

Thank you Image Analyst. 'mat2gray' workes well on the images.
Before and after using this : 255*( (Img - min(Img(:)))./max(img(:))), when I plot Image histogram it gives me a histogram with just two lines but in fact the image has different intensities. Also, after using this formula some images has only the intensity of 0. May I know why this happens ?
also for other images which initially have intensity [ 0 255] their histogram has just two or three lines. After using mat2gray the histograms were ok.
Attach the image where the image has only one intensity (zero) or two lines in the histogram. It could be that you're using floating point images and imhist(). imhist() assumes that if the image is floating point (not uint8 or uint16) that all values should be between 0 and 1. If you have any pixels with a value over 1, it will lump all of those into the last/right bin at intensity 1.
Image has ".nifti" format and I can not attach it here. I read it in MATLAB, saved it as .mat file and attached it here. The image is uint16 and it is not floating point.
SaHaR
If we use imhist(), the X axis will be from 0 to 65,535. But your image is not uint16. It is int16. So the gray levels go from -32768 to +32767. But this image does not have all that many unique gray levels. It has fewer so most of its gray levels will be bundled into a few bins. The default number of bins is 256, which means each bin will hold 65535/256 = 256 gray levels. All your gray levels are in the range 0-658. So that's why all your gray levels are in 3 bins: one covering 0-255, the second covering 256-511, and the third covering 512-767. To see more bins, you should specify the bin width using histogram():
Try this:
s = load('image.mat')
grayImage = s.Image;
% This is a SIGNED int16 array, not unsigned uint16 array.
[rows, columns, numberOfSlices] = size(grayImage);
for k = 1 : numberOfSlices
thisSlice = grayImage(:, :, k);
% Display the image.
subplot(numberOfSlices, 2, 2*(k-1)+1);
imshow(thisSlice, []);
axis('off', 'image');
% Display the histogram.
subplot(numberOfSlices, 2, 2*(k-1)+2);
[counts, binLocations] = imhist(thisSlice);
grid on;
% If we use imhist(), the X axis will be from 0 to 65,535
% but this image does not have that many gray levels.
% It has fewer and most of its gray levels will be bundled into a few bins.
% The default number of bins is 256, which means each bin will hold 65535/256 = 256 gray levels.
% Find out what the min and max actually are:
minGL = min(thisSlice(:)); % Turns out to be 0 for every slice.
maxGL = max(thisSlice(:)); % Turns out to be 658 in slice #3.
fprintf('For slice %d, min GL = %d and max GL = %d.\n', k, minGL, maxGL);
% Make the histogram go from 0 to 700:
edges = linspace(0, 700, 100); % 100 bins
histogram(thisSlice, edges);
grid on;
drawnow;
end
That's true.
Thank you Image Analysit. You always give correct and comprehensive answers :)

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 29 Jul 2020

Edited:

on 9 Aug 2020

Community Treasure Hunt

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

Start Hunting!