Converting madnrill into gray scale image.

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

 Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Mayo de 2015
Editada: Walter Roberson el 24 de Mayo de 2015
ind2gray(X, map)

4 comentarios

Talha Khan
Talha Khan el 24 de Mayo de 2015
Sorry i did not watch the command right, i think it is ind2gray(X,map).
Let me check sir.
Talha Khan
Talha Khan el 24 de Mayo de 2015
Sir,
I checked it, like this
y=ind2gray(X,map);
image(y)
it is displaying a blue picture
Talha Khan
Talha Khan el 24 de Mayo de 2015
okay, its done. Thank you very much sir
I used imshow(y) instead of image(y)
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)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 24 de Mayo de 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 comentarios

Walter Roberson
Walter Roberson el 24 de Mayo de 2015
That turns out not to be the issue. ind2gray() produces a single-component grayscale image, and image() by default handles all single-component arrays as being indexed images, indexed into the current color map. inshow() on the other hand does not deal with indexed images and so treats the single-layer array as grayscale.
The data for madrill is double() that is integral valued, 1 to 220.
Image Analyst
Image Analyst el 24 de Mayo de 2015
You're right - I never use image(). You have to expand the twisty way down in the help to discover that monochrome arrays are assumed to be indexed so I would guess that most people don't know that. I always use imshow(), and most people should now that it's built into base MATLAB.
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 .
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Color and Styling en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Mayo de 2015

Comentada:

el 26 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by