【问题标题】:Matlab - attach scalar value to RGB coloursMatlab - 将标量值附加到 RGB 颜色
【发布时间】:2011-09-18 14:06:26
【问题描述】:

当我将以下图像读入 Matlab 时,我得到一个 3D 矩阵,它基本上包含组成图像中每个像素的 RGB 颜色样本的值。


是否有任何 Matlab 函数可用于根据 RGB 值为图像中的每个像素分配一个标量值,例如 [-10, 10]?纯红色应该是10,黄色应该是5,绿色应该是0,蓝色应该是-8,青色应该是-10。

【问题讨论】:

  • 其他颜色呢? (例如,不“纯”的颜色)。
  • 在我的图像中,我将只有这个色谱。
  • 见rgb2ind

标签: matlab


【解决方案1】:

看看 RGB2IND:http://www.mathworks.com/help/techdoc/ref/rgb2ind.html

然后您可以将 Nx3 索引输出替换为您自己的自定义 N 元素索引向量。

【讨论】:

    【解决方案2】:

    从运行中可以看出

    colorlist=[1 0 0;1 1 0;0 1 0; 0 0 1; 0 1 1];
    valuelist=[10 5 0 -8 -10];
    figure;
    hold all;
    for i=1:10
      bar(i,i,'FaceColor',colorlist(i,:))
    end;
    

    上面定义的colorlist对应你感兴趣的颜色。

    要解决您的问题,对于图像中的每个像素,您必须确定哪些 RGB 值正好为零,哪些正好是 1,以确定要在哪一对之间进行插值。例如,假设size(image)==[100,100,3] 和image=im2double(imread(myfilename)) 即max(image(:))==1:

    if ((image(x,y,:)==0)==[0 0 1]) && ((image(x,y,:)==1)==[1 0 0])
        %# interpolate between red and yellow
        result(x,y)=10 - 5*image(x,y,2); %# pure red gives 10, pure yellow gives 5
    elseif ((image(x,y,:)==0)==[0 0 1]) && ((image(x,y,:)==1)==[0 1 0])
        %# interpolate between yellow and green
        result(x,y)=5*image(x,y,1); %# pure yellow gives 5, pure green gives 0
    elseif 
        %# ...
    end
    

    这个解决方案不是矢量化的,但它应该能让您走上正确的轨道,以实现可行的实施。当然,如果您可以避免使用多色查找表将数据保存为 RGB,而是保存原始值,则可以省去一些麻烦...

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 2016-04-06
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-04
      • 2017-01-02
      • 1970-01-01
      • 2018-06-21
      相关资源
      最近更新 更多