Converting madnrill into gray scale image.

조회 수: 6 (최근 30일)
Talha Khan
Talha Khan 2015년 5월 24일
댓글: Walter Roberson 2015년 5월 26일
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

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 24일
편집: Walter Roberson 2015년 5월 24일
ind2gray(X, map)
  댓글 수: 4
Talha Khan
Talha Khan 2015년 5월 24일
okay, its done. Thank you very much sir
I used imshow(y) instead of image(y)
Walter Roberson
Walter Roberson 2015년 5월 24일
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)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 5월 24일
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
Walter Roberson
Walter Roberson 2015년 5월 24일
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
Walter Roberson 2015년 5월 26일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Color and Styling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by