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

Sam*_*Sam 4 matlab colors image-processing

我正在尝试在MATLAB中生成一个颜色图,给出三种颜色,一种是高极限,零极低.我的思维过程是从高端到中间循环并将每一步存储到a 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
Run Code Online (Sandbox Code Playgroud)

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

Sha*_*hai 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
Run Code Online (Sandbox Code Playgroud)