How do I specify a specific color for a specific value when using the colormap?

58 次查看(过去 30 天)
I have a matrix that I would like to plot using imagesc. I have done this and then used one of the standard colormaps (jet, summer, etc.). My matrix is a 75x75 matrix and the matrix consist of only five different values that I would like to plot in five specific colors.
For example -1 = blue, 3 = green, 4 = grey, 5 = Brown and 10 = red.
I have only managed to create a range of colors by using the standard colormaps (ranging from blue to red for example). I tried to create my own colormap, however I could only involve three different colors. When I tried to involve four colors I received an error saying that there was something wrong with my dimensions.
In Excel this is very simple and I guess it's the same in Matlab and that I'm missing something very basic here?

采纳的回答

Image Analyst
Image Analyst 2018-8-23
Try this:
% Create indexed image.
indexedImage = randi(10, 75, 75);
% Set values other than 1, 3, 4, 5, 10 to 1.
indexedImage(indexedImage == 2 | (indexedImage >= 6 & indexedImage <= 9)) = 1;
% Now we have a sample image like the poster has.
subplot(1, 2, 2);
histogram(indexedImage);
grid on;
% Create colormap with special colors for 1, 3, 4, 5, and 10
% and black for any other values (of which there shouldn't be any).
cmap = zeros(10, 3);
cmap = [0, 0, 1; ... % Blue for 1
0, 0, 0; ... % Black for 2
0, 1, 1; ... % Green for 3
0.5, 0.5, 0.5; ... % Gray for 4
0.5, 0.5, 0; ... % Brown for 5
0, 0, 0; ... % Black for 6
0, 0, 0; ... % Black for 7
0, 0, 0; ... % Black for 8
0, 0, 0; ... % Black for 9
1, 0, 0] % Red for 10
% Display the image with the colormap applied.
subplot(1, 2, 1);
imshow(indexedImage, cmap);
axis on;
colorbar(gca);

更多回答(0 个)

类别

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