【问题标题】:Create image mask based on range of pixel value根据像素值范围创建图像蒙版
【发布时间】:2018-07-01 12:12:23
【问题描述】:

我需要屏蔽我的图像。所有像素值都不是

R=170-220 G=100-150 B=60-100

应该被屏蔽为黑色。

可以通过循环查找像素值来做到这一点,如果它超出了R或G或B的范围,则将其替换为黑色像素,否则保持其原始值,但成本很高。如何避免 if 循环功能。 matlab有内置函数吗?

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    您可以使用 Matlab 的逻辑索引功能。 它允许您根据不同的逻辑语句(包括基于数组元素值本身的逻辑语句)对数组进行索引。

    Image = imread('ImageName.extension');
    %Read Image
    
    R_Channel = Image(:,:,1);
    G_Channel = Image(:,:,2);
    B_Channel = Image(:,:,3);
        %Isolate Colour Channels.
    
        %R=170-220 G=100-150 B=60-100
    
        %Logical Indexing
    R_Channel(R_Channel < 170 | R_Channel > 220)  = 0;   %Black Mask
    G_Channel(G_Channel < 100 | G_Channel > 150)  = 0;   %Black Mask
    B_Channel(B_Channel < 60  | B_Channel > 100)  = 0;   %Black Mask
        %Apply constraint on each Channel
    
    MaskedImage = cat(3,R_Channel,G_Channel,B_Channel);
        %Merge all Colour channel to get the masked Image
    

    【讨论】:

      【解决方案2】:

      改为使用逻辑索引。例如R频道:

      R = img(:,:,1);
      R_Constraint = R >= 170 && R <= 220;
      R(R_Constraint) = 1;
      R(~R_Constraint) = 0;
      

      分别为GB 执行此操作。然后,使用元素并获得最终结果为黑白图像。

      G = img(:,:,2);
      G_Constraint = G >= 100 && G <= 150;
      G(G_Constraint) = 1;
      G(~G_Constraint) = 0;
      
      B = img(:,:,3);
      B_Constraint = B >= 60 && B <= 100;
      B(B_Constraint) = 1;
      B(~B_Constraint) = 0;
      
      img = R & G & B;
      img(img == 1) = 255;
      img = uint8(img); 
      

      【讨论】:

        猜你喜欢
        • 2013-07-20
        • 2021-10-02
        • 2020-12-01
        • 2013-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        相关资源
        最近更新 更多