如何将单个颜色设置为大于0但小于1的值?

时间:2016-10-09 13:52:20

标签: matlab

我有一个nxn矩阵,例如:

[ 0   1    1 ;
 0.2  1   0.1; 
  0  0.4   0] 

我想要想象我的矩阵,我想:

  • 所有值= 1为黑色
  • 0和1之间的所有值(0 <值<1)为白色
  • 所有值= 0为特定颜色(例如红色)。

如下图所示:

Example

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

以下解决方案构建色彩映射,并使用ind2rgb创建RGB图像:

  • 将A转换为&#34;索引图像&#34; (扩展256,并且舍入) - 索引图像元素必须是整数。
  • 创建色彩映射会议范围条件。
  • 使用ind2rgb将X转换为带有创建的色彩映射的RGB图像。

检查以下代码示例:

A = [  0    1    1;...
     0.2    1  0.1;...
       0  0.4    0];

%N - Number of elements in the color map (e.g 256) applies "quantization level".
N = 256;

%Convert A to "indexed image" in range [0, N].
X = round(A*N);

%Create color map with N elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%R, G, B (Red, Green, Blue) applies three color components of color map.
R = zeros(N, 1);
G = zeros(N, 1);
B = zeros(N, 1);

%Create array of 100 values in range 0 to 1
V = linspace(0, 1, N);

%All values = 1 to be black
R(V == 1) = 0;
G(V == 1) = 0;
B(V == 1) = 0;

%All values between 0 and 1 (0 < value <1) to be white
R((V > 0) & (V < 1)) = 1;
G((V > 0) & (V < 1)) = 1;
B((V > 0) & (V < 1)) = 1;

%All values = 0 to be a specific color (red for example).
R(V == 0) = 1;
G(V == 0) = 0;
B(V == 0) = 0;

%Concatenate color components, and form Nx3 color map.
cmap = [R, G, B];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Convert A to RGB, using colormap cmap.
RGB = ind2rgb(X, cmap);

imshow(RGB);

解决方案不是最简单的解决方案,但它可用于解决更一般的可视化问题。

结果(放大):
enter image description here