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

58 Ansichten (letzte 30 Tage)
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?
  2 Kommentare
Geoff Hayes
Geoff Hayes am 23 Aug. 2018
Robin - how did you construct your custom colormap? Something like
customColormap = [0 0 1; 0 1 0; 0.5 0.5 0.5; 0.5 0 0; 1 0 0];

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 23 Aug. 2018
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);

Weitere Antworten (0)

Kategorien

Mehr zu Color and Styling finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by