Converting madnrill into gray scale image.
6 views (last 30 days)
Show older comments
Talha Khan
on 24 May 2015
Commented: Walter Roberson
on 26 May 2015
Hi everyone, I have loaded mandrill and display it using these commands
>>load mandrill
>> colormap(map)
>> image(X)
Now I want to change this image into gray scale. Can anyone please tell me how to do this??
Thanks in anticipation
0 Comments
Accepted Answer
Walter Roberson
on 24 May 2015
Edited: Walter Roberson
on 24 May 2015
ind2gray(X, map)
4 Comments
Walter Roberson
on 24 May 2015
gray scale images are usually single plane because all of the planes are the same. If you want you could use
y = repmat( ind2gray(X,map), 1, 1, 3);
image(y)
More Answers (1)
Image Analyst
on 24 May 2015
Your "y" was a floating point image in the range 0-1. You forgot to convert to uint8 for image(). imshow() can do it if you do imshow(y, []). image() can do it if you convert to uint8 and apply the right colormap:
% Load data.
load mandrill
colormap(map)
% Convert to grayscale in the range 0-1.
y=ind2gray(X,map);
% Convert to grayscale uint8 in the range 0-255
y = uint8(255 * mat2gray(y));
% Display
image(y)
% Apply a colormap appropriate for gray scale images.
colormap(gray(256));
4 Comments
Walter Roberson
on 24 May 2015
In the Examples it says,
By default, the CDataMapping property for the image is set to 'direct' so image interprets values in C as indices into the colormap. For example, the bottom right pixel corresponding to the last element in C, 22, uses the 22nd color of the colormap.
But of course "direct" starts numbering from 0, so C value 0 is the 1st color entry, C value 1 is the 2nd color entry, C value 22 is the 23rd color entry, not the 22nd.
I'll post some feedback on the page in hopes that it will be corrected. Last time I did that the destination address for the feedback showed up as being some kind of customer service company, so I don't know how much if it gets through to the developers / documenters .
Walter Roberson
on 26 May 2015
I did get some response from my feedback. They pointed out that the base for the mapping index depends on the datatype, and as the datatype in that example was double, the example wording was correct. The description accounting for datatype is here. What happens mostly makes sense when you read it, but some of the behaviours are obscure like logical 1 mapping into the second color in the colormap rather than the last color (what I would expect)
'direct' — Interpret the values as indices into the current colormap. Values with a decimal portion are fixed to the nearest lower integer.
If the values are of type double or single, then values of 1 or less map to the first color in the colormap. Values equal to or greater than the length of the colormap map to the last color in the colormap.
If the values are of type uint8, uint16, uint32, uint64 , int8, int16, int32, or int64, then values of 0 or less map to the first color in the colormap. Values equal to or greater than the length of the colormap map to the last color in the colormap (or up to the range limits of the type).
If the values are of type logical, then values of 0 map to the first color in the colormap and values of 1 map to the second color in the colormap.
See Also
Categories
Find more on Color and Styling 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!