How do I map an array to grayscale colormap?

30 views (last 30 days)
I have an array of numbers and would like to:
1) map 0 to a display intensity of 0 and the highest value in the array to white with a display intensity of 255, with the numbers of the array in between mapped to display between the grayscale range 0-255.
2) map the median number in the array to display an intensity of 255, with anything higher than the median number being displayed with an intensity of 255, and anything lower than the median number being displayed with a pixel intensity between 0-255.
I think I might have 1) figured out if just using colormap('gray') automatically gives the highest number in the array the brightest intensity, but in my image I dont see anything bright white, just shades of gray.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Aug 2013
1)
maxv = max(YourArray(:));
mapped_array = uint8((double(YourArray) ./ maxv) .* 255);
image(mapped_array);
colormap(gray(255));
2)
maxv = median(YourArray(:));
mapped_array = uint8((double(YourArray) ./ maxv) .* 255);
image(mapped_array);
colormap(gray(255));

More Answers (1)

Image Analyst
Image Analyst on 26 Aug 2013
For #1, if you have the Image Processing Toolbox, you can simply do
imshow(grayImage, [0, max(grayImage(:)]);
You can get even more contrast if the image's lowest value is not zero by doing this:
imshow(grayImage, []);
For #2, it's similar:
imshow(grayImage, [0, median(grayImage(:)]);
These methods do not change grayImage (your original image) nor do they necessitate the creation of a temporary image used only for display.
  5 Comments
Walter Roberson
Walter Roberson on 28 Aug 2013
In general, the default colormap size is 64 entries; I do not have IPT so I do not know what imshow() would do.
Image Analyst
Image Analyst on 28 Aug 2013
The numbers in the brackets take the left number and make that 0 in the display, and the right number and make that show up as 255 and linearly scaled over the 254 gray levels in between. If it's [] then it sends the min of the image to 0 and the max of the image to 255.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!