Converting grayscale image to indexed

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

 Respuesta aceptada

Image Analyst
Image Analyst el 19 de Ag. de 2018
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 comentarios

Micha
Micha el 19 de Ag. de 2018
Thank you for clarifying this use. I wasn't aware that the values in the pixels correspond to their position in the colormap, from whence the colors are taken.
As a follow-up, I was wondering if it is possible to accomplish this without having to set cmap(92, :) = [1, 1, 0]; cmap(124, :) = [1, 0, 1]; cmap(256, :) = [1, 0, 0]; as you have shown, but rather have it in order - so that the order of the grayscale values would correspond to the order in the cmap, so that I can initially have cmap = [1 1 0; 1 0 1; 1 0 0]; this would have worked if I converted 91, 123, 255 in the original image to 0, 1, 2, which is the reason for my initial question.
Image Analyst
Image Analyst el 19 de Ag. de 2018
You can but it's a lot of extra code that only complicates things. Why would you want to do that instead of the very simple method I gave you?
Micha
Micha el 19 de Ag. de 2018
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
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]);

Iniciar sesión para comentar.

Más respuestas (1)

Micha
Micha el 19 de Ag. de 2018

0 votos

Thanks, imquantize was what I was looking for.

Categorías

Productos

Versión

R2017a

Etiquetas

Preguntada:

el 19 de Ag. de 2018

Respondida:

el 19 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by