How to convert a range of pixel values to another range(0-255) ?
58 views (last 30 days)
Show older comments
Pratik Datta
on 12 Feb 2015
Commented: cecilia rodrigues
on 25 Feb 2021
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?
0 Comments
Accepted Answer
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
on 24 Mar 2020
u can also normalize your image in 0 to 1 range, then uint8(normalizedimage*255).
Image Analyst
on 24 Mar 2020
You can also use the new rescale() function instead of mat2gray() if you want.
More Answers (1)
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
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!
See Also
Categories
Find more on Geometric Transformation and Image Registration 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!