custom colormap with missing colors

If I create the following image, with 3 possible values: 1, 2 and 10.
im = zeros(128,128)
im(1:32,:) = 1
im(33:64,:) = 2
im(65:end,:) = 10
ax = imshow(im,[])
%I apply the following colormap:
cmap = [0.9020 0.0980 0.2941
0.2353 0.7059 0.2941
0.8000 0.8000 0.8000]
colormap(ax.Parent,cmap)
I obtain an image with only 2 colors, when the colormap has 3 colors. Why isn't the colormap being respected? I want these specific colors for each value of the image. And I want my colormap to include only these colors, no more and no less. And no, my image does not contain values from 3 to 9. It is just 1, 2 and 10 and it is supposed to be like this.
Any help appreciated
André

 Respuesta aceptada

Stephen23
Stephen23 el 4 de Jul. de 2018
Editada: Stephen23 el 4 de Jul. de 2018
You could:
1. specify the limits, and provide the axes handle when calling colormap:
imshow(im,[1,3]);
colormap(gca,cmap)
2. treat it as an indexed image (note that out-of-range indices are truncated to the end colors):
>> imshow(im,cmap)
3. convert it to a TrueColor (RGB) image:
>> [~,idx] = ismember(im,[1,2,10]);
>> rgb = ind2rgb(idx,cmap);
>> imshow(rgb)

3 comentarios

André
André el 4 de Jul. de 2018
Editada: André el 4 de Jul. de 2018
Nice answer. Unfortunately it didn't help me that much, since I am working with more complex images. Imagine I have an image with the values: 1,2,3,8,9,10. That way option 1 would not work. Option 2 I haven't tested, since my image is actually created using the "patch" function, not "imshow" (used here just to simplify the question). Converting to RGB is out of question since I want a colorbar displayed.
The idea is to display patches with data of different categories, each category with one color. Categories are encoded in the CData of the vertexes. Like this:
It must adapt for any number of groups (up to 8), with or without overlaps, and with or without outside areas. The order of the colors must be always the same. Doing this is not a simple task.
I solved the problem by creating 2 axes. One of the axes has only the colorbar, where I included only the colors I wanted. The second axes contains the patch, where I inserted rows of zeros in its colormap to fill the missing indexes. The rows of zeros do not appear on the colorbar, since it is a copy without the rows of zeros.
Thanks for the answer anyway.
Stephen23
Stephen23 el 4 de Jul. de 2018
Editada: Stephen23 el 4 de Jul. de 2018
@André: Thank you. You might like to read these:
You can easily specify the patch 'CDataMapping' to be 'direct', in which case the patch CData corresponds one-to-one with the axes colormap, as does the colorbar. Like this:
>> cmap = [0,0,1;0,1,0;1,0,0;0,1,1;1,1,0;1,0,1]; % six ugly colors.
>> X = [0,1,0,1;1,2,1,2;1,2,1,2;0,1,0,1]; % four squares
>> Y = [0,0,1,1;0,0,1,1;1,1,2,2;1,1,2,2]; % four squares
>> patch(X,Y,[1;2;3;6],'FaceColor','flat','CDataMapping','direct')
>> colormap(cmap)
>> colorbar()
>> caxis([0.5,6.5])
Note the third input to patch: you can see how each patch C value corresponds to the colormap row/color. Note how all colormap colors are shown exactly in the colorbar, in the correct order:
A colorbar is just a narrow axes on one side of the figure, so you could easily create this yourself, which gives you complete control over its location, size, tick labels, etc.
André
André el 10 de Jul. de 2018
Exactly what I needed. Thanks a lot.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Preguntada:

el 4 de Jul. de 2018

Comentada:

el 10 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by