How do I map an array to grayscale colormap?

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

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)

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

Except that Richard did not specify that the maximum array value was to map to the maximum color intensity; he specified that it had to map to 255. We are not told why 255 specifically.
The sort of situation in which it would make a difference is if data cursor mode is used; imshow() used the way you use it would show intensities or z values according to the actual values stored in the array, whereas the version I gave with creating a temporary array would have data cursor mode show the mapped values instead.
Your version is probably what Richard wants, but it isn't quite what Richard asked for ;-)
I'm assuming his max display value is 255 and he's not using one of those fancy monitors like they use in radiography, which have 1024 or 4095 or however many gray levels they can do these days. I'm not sure if imshow() is smart enough to figure out what the greatest gray level your display can use is, but maybe it is. If he is using an extended range monitor and imshow() knows about it, then some adjustment in the code or the MATLAB preferences/settings may be required if he really wants 255 instead of the max the monitor/adapter can handle.
Thanks for the help, I don't have the image processing toolbox so I went with Walt's method and it worked
In general, the default colormap size is 64 entries; I do not have IPT so I do not know what imshow() would do.
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!