Convert the intensity of image into a specific intensity range
20 views (last 30 days)
Show older comments
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
Rik
on 2 Aug 2020
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.
Accepted Answer
Image Analyst
on 2 Aug 2020
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
Image Analyst
on 7 Aug 2020
Edited: Image Analyst
on 7 Aug 2020
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/343084/image.png)
More Answers (0)
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!