在matlab中创建一个定制的颜色条

时间:2012-06-27 15:37:47

标签: matlab colorbar

我必须创建一个地图,以显示某些值距离范围有多近,并为其提供颜色。同时,在该范围内的值应该具有另一种不同的颜色 例如:只有[-2 2]范围内的结果才能被视为有效。对于其他值,颜色必须显示与这些限制的距离(-3比-5更亮,更暗)
我尝试使用 colorbar ,但我无法设置自定义的色标。
任何的想法?? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

您需要为您拥有的值范围定义色彩图 色图是N * 3矩阵,定义每种颜色的RGB值。

请参阅下面的示例,范围为-10:10,有效值为v1,v2:

v1=-3;  
v2=+3;  
a = -10:10;  
graylevels=[...  
   linspace(0,1,abs(-10-v1)+1) , ...
   ones(1, v2-v1-1) , ...
   linspace(1,0,abs(10-v2)+1)];  
c=repmat(graylevels , [3 1])';  
figure;  
imagesc(a);  
colormap(c);

答案 1 :(得分:0)

以下是一些代码,我将它们放在一起,演示了一种创建自己的查找表并将值从中分配给您正在使用的图像的简单方法。我假设您的结果是在2D数组中,我只是使用随机分配的值,但概念是相同的。

我提到使用HSV作为着色方案。请注意,这需要你有一个m乘n乘3矩阵。顶层是H - 色调,第二层是S - 饱和度,第三层是V或值(亮/暗)。只需将H和S设置为您想要的颜色值,并以类似的方式改变V,如下所示,您可以获得所需的各种浅色和深色。

% This is just assuming a -10:10 range and randomly generating the values.
randmat = randi(20, 100);
randmat = randmat - 10;

% This should just be a black and white image.  Black is negative and white is positive.
figure, imshow(randmat) 

% Create your lookup table.  This will illustrate having a non-uniform
% acceptable range, for funsies.
vMin = -3;
vMax = 2;

% I'm adding 10 here to account for the negative values since matlab
% doesn't like the negative indicies...
map = zeros(1,20); % initialized
map(vMin+10:vMax+10) = 1; % This will give the light color you want.

%linspace just simply returns a linearly spaced vector between the values
%you give it.  The third term is just telling it how many values to return.
map(1:vMin+10) = linspace(0,1,length(map(1:vMin+10)));
map(vMax+10:end) = linspace(1,0,length(map(vMax+10:end))); 

% The idea here is to incriment through each position in your results and
% assign the appropriate colorvalue from the map for visualization.  You
% can certainly save it to a new matrix if you want to preserve the
% results!
for X = 1:size(randmat,1)
    for Y = 1:size(randmat,2)
        randmat(X,Y) = map(randmat(X,Y)+10);
    end
end

figure, imshow(randmat)