MATLAB:生成给定三种颜色的色彩图

时间:2013-06-24 15:30:54

标签: matlab image-processing colors

我正在尝试在MATLAB中生成一个色彩图,给出三种颜色,高极值,零极值和低极值。我的思维过程是从高端到中间循环并将每一步存储到3xN(第一列是R,第二列是G,第三列是B)矩阵。所以我正在使用:

%fade from high to zero
oldRed=high(1);
oldGreen=high(2);
oldBlue=high(3);
newRed=mid(1);
newGreen=mid(2);
newBlue=mid(3);

currentRed=oldRed; currentGreen=oldGreen; currentBlue=oldBlue;
for x=1:steps
    currentRed=oldRed+((x*(newRed-oldRed))/(steps-1));
    currentGreen=oldGreen+((x*(newRed-oldRed))/(steps-1));
    currentBlue=oldBlue+((x*(newRed-oldRed))/(steps-1));
    cmap=[cmap;[currentRed currentGreen currentBlue]];
end

然后我会做同样的事情,从零值到低端。但是我的代码并没有给我任何有用的矩阵。有人能帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:5)

您可以使用线性插值来扩展颜色

 nCol = 256; % number of colors for the resulting map
 cmap = zeros( nCol, 3 ); % pre-allocate
 xi = linspace( 0, 1, nCols );
 for ci=1:3 % for each channel
     cmap(:,ci) = interp1( [0 .5 1], [low(ci) mid(ci) high(ci)], xi )';
 end

答案 1 :(得分:3)

受@ Shai的回答启发,这是他的解决方案的一个小转折(我更喜欢它 - 它更灵活,并且避免使用for循环)。

您想要的cmap形式是nx3数组。此外,您说您有三种颜色可以表示曲线上的三个“断点”。这尖叫“插值”!

% set the "breakpoints" for the color curve:
lowValue = 0;
midValue = 128;
highValue = 255;

% pick "any" three colors to correspond to the breakpoints:
lowColor = [255 0 0];
midColor = [40 40 40];
highColor = [0 255 255];

% create the colormap:
myMap = interp1( [lowValue midValue highValue], ...
  [lowColor; midColor; highColor]/255, ...
  linspace(lowValue, highValue, 256));

这确保了具有256种颜色的地图,从最低值{index 1到色彩图的lowColor平滑到最高值的highColor(颜色图中的索引255)。

我相信这正是您所寻找的。并且“看起来马,没有循环!”。

答案 2 :(得分:1)

我会使用linspace:

cmap=[linspace(oldRed,newRed,steps)' ...
linspace(oldGreen,newGreen,steps)' ...
linspace(oldBlue,newBlue,steps)'];

然后为下一步做同样的事情,并将它们连接起来:

cmap_full = [cmap;cmap2];
相关问题