# I have a ct image that values of pixel of it is between 0-4000 how can I convert it to an image with 0-255 ?

12 views (last 30 days)
sara on 5 Sep 2014
Commented: Image Analyst on 30 Jul 2020
I have a ct image that values of pixel of it is between 0-4000 how can I convert it to an image with 0-255 ?
##### 2 CommentsShow NoneHide None
sara on 5 Sep 2014
ok thanks
I think I ask my question in an other way... I should ask:how can I convert HU(hounsfield units 0-4095) to grayscale(0-255)...
Iain on 5 Sep 2014
img = double(HU);
img = img - min(img(:));
maximum = max(img(:));
grayscale = (img / maximum) * 255;

Sign in to comment.

### Accepted Answer

Image Analyst on 5 Sep 2014
There is a function that scales an array to 0-1. It's called mat2gray. Then just multiply by 255.
image8bit = uint8(255 * mat2gray(image16bit));
##### 4 CommentsShow 2 older commentsHide 2 older comments
SaHaR on 30 Jul 2020
Dear Image Analyst,
yesterday I asked the same question. Your solutions are always the best. Thank you.
Image Analyst on 30 Jul 2020
There is now another way, using the new rescale() function:
grayImage = uint16([10, 4000, 65535]) % Sample uint16 image.
b8 = rescale(grayImage, 0, 255) % If you want double
class(b8)
b8 = uint8(rescale(grayImage, 0, 255)) % If you want uint8
class(b8)
grayImage =
1×3 uint16 row vector
10 4000 65535
b8 =
0 15.528 255
ans =
'double'
b8 =
1×3 uint8 row vector
0 16 255
ans =
'uint8'

Sign in to comment.

### More Answers (2)

Guillaume on 5 Sep 2014
If the image is stored as double, then Yawar's answer is correct. If your image is stored as uint16, then it won't work, you'll have to convert the image to double first.
For most image processing function to work correctly on an image in the range 0-255, it needs to be of type uint8. If the type is double, most functions assume the range 0-1.
The following is guaranteed to work:
img8bit = im2uint8(double(img) / 4000);
This is equivalent to:
img8bit = uint8(double(img)/4000 * 255);
##### 3 CommentsShow 1 older commentHide 1 older comment
Guillaume on 5 Sep 2014
I'm not sure what you mean by original size. The size of the image isn't changed, just the intensity range and the type.
If you want to go back from a uint8 image to an image in the range 0-4000:
img12bitish = double(img8bit) / 255 * 4000;
%or to store it as uint16
img12bitish = uint16(double(img8bit) / 255 * 4000);
Your code return16bit = im2uint16(img8bit) will convert the image to the range 0-65535.
Christin Panjaitan on 5 Sep 2014
I have tried your code.
[1] img12bitish = double(img8bit) / 255 * 4000; %it might return to the original range but the original value has been changing.
For the word of "size", I mean range. Sorry, I just wrong to use the word.

Sign in to comment.

Yawar Rehman on 5 Sep 2014
img = (img / 4000) * 255;
##### 1 CommentShow -1 older commentsHide -1 older comments
Iain on 5 Sep 2014
This'd be better....
img = double(img);
img = img - min(img(:));
maximum = max(img(:));
img = (img / maximum) * 255;

Sign in to comment.

### Categories

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

### Community Treasure Hunt

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

Start Hunting!