adjusting color in colorbar automatically

6 views (last 30 days)
chess
chess on 26 Aug 2014
Answered: Image Analyst on 27 Aug 2014
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:

Answers (3)

Image Analyst
Image Analyst on 27 Aug 2014
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 on 26 Aug 2014
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 Comments
chess
chess on 26 Aug 2014
can you please tell me what is cptcmap?I have never seen anything like this.
Kelly Kearney
Kelly Kearney on 27 Aug 2014
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:

Sign in to comment.


Sean de Wolski
Sean de Wolski on 26 Aug 2014
You could also try the colormap editor:
>>colormapeditor
(Also available in the figure under "Edit" -> "Colormap")
  1 Comment
chess
chess on 26 Aug 2014
I know about that I like to do it using code in the command window.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!