How to convert a range of pixel values to another range(0-255) ?

58 views (last 30 days)
I have a DICOM image in which pixel values ranges from -2000 to 2666. I need to convert those values to ones which are within the grayscale range. How can i do this?

Accepted Answer

Image Analyst
Image Analyst on 12 Feb 2015
Try mat2gray(), which converts any matrix to a 0-1 range. Then multiply by 255 and cast to uint8 if you want a uint8 data type.
uint8Image = uint8(255 * mat2gray(yourDicomImage));
  3 Comments
tarek bentahar
tarek bentahar on 24 Mar 2020
u can also normalize your image in 0 to 1 range, then uint8(normalizedimage*255).
Image Analyst
Image Analyst on 24 Mar 2020
You can also use the new rescale() function instead of mat2gray() if you want.

Sign in to comment.

More Answers (1)

Jan
Jan on 12 Feb 2015
Edited: Jan on 12 Feb 2015
Img1 = randi([-2000, 2666], 640, 480);
Img2 = round((Img1 + 2000) * (255 / 4666));
  1 Comment
cecilia rodrigues
cecilia rodrigues on 25 Feb 2021
Hello Jan,
Why do I get different pixel value results when I use the formula you suggested above and the wcodemat(image,255,'mat',1) function?
Shouldn't wcodemat also scale from 0 to 255?
Thank you very much!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!