MATLAB色彩映射问题,值为0

时间:2014-02-06 14:07:18

标签: matlab color-mapping

我已经在网上搜索并堆叠溢出来寻找这个(看似简单的)问题的答案,但是找不到答案:

我正在编写MATLAB中的元胞自动机。我使用的n * m矩阵的值介于0到15之间,我使用带有5个灰度值(0到1之间)的色彩图来制作图像。请参阅以下代码段以阐明这一点:

WIDTH = 100;
HEIGHT = 100;

fields = randi(16,HEIGHT,WIDTH)-1;
% here the grey values 0, 0.25, 0.5, 0.75 and 1 are mapped to the values 1 to 16
cmRow = [1;0.75;0.75;0.5;0.75;0.5;0.5;0.25;0.75;0.5;0.5;0.25;0.5;0.25;0.25;0];
specialGray = [cmRow, cmRow, cmRow];
colormap(specialGray);
image(fields)

我的问题是,如果出现值0,那么MATLAB将使用的色彩映射表中没有第0行。结果,总会缺少一种颜色。 遗憾的是,仅使用1到16而不是0到15的值是一种选择,因为我后来在脚本中严重依赖这些值。 有什么明显的东西,我不见了?您对如何解决这个问题有任何想法吗?

非常感谢! 最好的祝福, 勒

1 个答案:

答案 0 :(得分:1)

image函数知道两种颜色映射:direct' (the default) and 'scaled'. If you use 'scaled', you can set the scale for the color with the caxis`函数。因此,下面的代码应该可以解决问题(但你当然也可以按照Oleg的建议转换值):

image(fields,'CDataMapping','scaled');
colormap(specialGray);
caxis([0 15]);