Converting grayscale image to indexed

조회 수: 5 (최근 30일)
Micha
Micha 2018년 8월 19일
답변: Micha 2018년 8월 19일
I have a grayscale image with 3 random values signified by integers 91, 123, 255. When I use gray2ind(I, 3) I get only 2 indices: 1 and 2. But when I use gray2ind(I, 2) I get 0 and 1 as expected. How come? I would expect gray2ind(I, 3) to give me 0, 1 and 2.
I do have a colormap array which I am planning to later use, which has 3 individual colors, such that after I would properly convert this to 0, 1 and 2 indices I'd just do imshow(gray2ind(I, 3), colors).
Thanks

채택된 답변

Image Analyst
Image Analyst 2018년 8월 19일
Don't worry about it. You can use a grayscale image as an indexed image as-is. Just set up the colormap based on the grayscale and set the map for whatever colors you want for the special values.
grayImage = imread('pout.tif');
% Initialize a normal colormap for a normal gray scale image.
cmap = gray(256);
% Set up special colors for 91, 123, 255.
cmap(92, :) = [1, 1, 0]; % Gray scale 91 will show up as yellow.
cmap(124, :) = [1, 0, 1]; % Gray scale 123 will show up as magenta.
cmap(256, :) = [1, 0, 0]; % Gray scale 255 will show up as red.
imshow(grayImage, cmap);
colorbar
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
However, note that the index (row) of the colormap you want to use is one more than the gray level, because row 1 controls the color of gray level 0, row 2 controls the color of gray level 1, and so on until row 256 controls the color of gray level 255.
  댓글 수: 4
Micha
Micha 2018년 8월 19일
Because I have other images that are indexed to begin with, so this method is compatible with them. The instance I described here is the case with some images, so it's more convenient to convert these to 0 1 2
Image Analyst
Image Analyst 2018년 8월 19일
Try this:
grayImage = imread('pout.tif');
% Quantize into 3 gray levels.
qImage = imquantize(grayImage, [91, 123, 255]);
unique(qImage)
% Set up a color map for color 1 valid for GL <=91,
% color 2 valid for GL >= 92 up to GL <= 123,
% and color 3 valid for GL >= 124 up to GL <= 255.
cmap = [1, 1, 0;
1, 0, 1;
1, 0, 0];
imshow(qImage, cmap);
colorbar
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

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

추가 답변 (1개)

Micha
Micha 2018년 8월 19일
Thanks, imquantize was what I was looking for.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by