adjusting color in colorbar automatically

조회 수: 4 (최근 30일)
chess
chess 2014년 8월 26일
답변: Image Analyst 2014년 8월 27일
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")
  댓글 수: 1
chess
chess 2014년 8월 26일
I know about that I like to do it using code in the command window.

댓글을 달려면 로그인하십시오.

카테고리

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