adjusting color in colorbar automatically

4 次查看(过去 30 天)
Hi Assume you have image with 4 colors in it and colormap is defined as :
cmap=[[0 0 1]; [1 0 0]; [0 1 0]; [1 1 0]]; I know range of my colorbar is from 0 to 1. Therefore each color is 0.25 range. meaning that it is from 0 to 0.25, 0.25 to 0.5 and so on. How can I do something that blue ([ 0 0 1]) is changed to 0 to 0.45 and for example red is changed to 0.45 to 0.60? something like this:

回答(3 个)

Image Analyst
Image Analyst 2014-8-27
See this demo:
clc;
workspace
grayImage = rand(200);
imshow(grayImage, []);
cmap=[[0 0 1]; [1 0 0]; [0 1 0]; [1 1 0]];
colormap(cmap);
colorbar;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
numberOfColors = 100; % Whatever, up to max of 256.
cmap = zeros(numberOfColors, 3);
% Bottom 45% of colors are blue.
index2 = round(0.45 * numberOfColors);
for row = 1 : index2
cmap(row, :) = [0, 0, 1];
end
% Gray levels in the range 0.45 - 0.65 set to red.
index1 = round(0.45 * numberOfColors);
index2 = round(0.65 * numberOfColors);
for row = index1 : index2
cmap(row, :) = [1, 0, 0];
end
% Gray levels in the range 0.65 - 0.8 set to green.
index1 = round(0.65 * numberOfColors);
index2 = round(0.8 * numberOfColors);
for row = index1 : index2
cmap(row, :) = [0, 1, 0];
end
% Gray levels in the range 0.8 - 1.0 set to yellow.
index1 = round(0.8 * numberOfColors);
index2 = numberOfColors;
for row = index1 : index2
cmap(row, :) = [1, 1, 0];
end
colormap(cmap);

Kelly Kearney
Kelly Kearney 2014-8-26
You'd have to repeat colors in the colormap the appropriate number of times. In this particular example, repeat each entry 9,4,3, and 4 times, respectively. My cptcmap function makes it easy to define colormaps with uneven intervals like this:
cmap = [[0 0 1]; [1 0 0]; [0 1 0]; [1 1 0]];
lim = [0 0.45 0.6 0.8 1];
ctable = [lim(1:end-1)' cmap*255 lim(2:end)' cmap*255];
save mycol.cpt ctable -ascii;
figure;
imagesc(rand(100));
cptcmap('mycol', 'mapping', 'direct', 'ncol', 20);
colorbar;
You really shouldn't need to specify the number of colors in this example (that option is supposed to be for colormap intervals that don't divide so nicely), but I just caught a bug in my own code... oops. Will update that right away.
  2 个评论
chess
chess 2014-8-26
can you please tell me what is cptcmap?I have never seen anything like this.
Kelly Kearney
Kelly Kearney 2014-8-27
It's one of my functions, posted on the FEX. I linked it in my post above, but here it is again, not quite as hidden:

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2014-8-26
You could also try the colormap editor:
>>colormapeditor
(Also available in the figure under "Edit" -> "Colormap")

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by