Matlab - 如何检测图像上的绿色?

时间:2016-06-07 16:44:30

标签: matlab colors image-segmentation

我在项目中工作,基本上我必须在图像上检测三分并删除其他信息。我使用HSV作为分段,并使用函数regionprops来检测每个元素。它工作正常,但在有屋顶的情况下,它们不会被删除,因为Hue的值类似于三个。到目前为止,这是结果: enter image description here

为了移除屋顶,我想也许可以检测到检测到的每个区域的绿色。如果该区域没有70%的绿色(例如)该区域被删除。我怎样才能做到这一点?如何仅检测图像的绿色?

1 个答案:

答案 0 :(得分:4)

解决方案说明

评估补丁中的绿色等级是一个有趣的想法。我建议采用以下方法:

  1. 将补丁从RGB转换为HSV颜色系统。在HSV颜色系统中,通过检查第一个通道,可以更容易地评估每个像素的色调(或 - 颜色)。

  2. 在色调系统中查找绿色范围。在我们的例子中它大概是[65 / 360,170 / 360],可以看出here

  3. enter image description here

    1. 对于每个补丁,计算有多少像素具有绿色范围内的色调值,并除以连接组件的大小。
    2. 代码Expamle

      以下功能评估"绿色等级"在补丁中:

      function [ res ] = evaluateLevelOfGreen( rgbPatch )
      %EVALUATELEVELOFGREEN Summary of this function goes here
      %   Detailed explanation goes here
      
      %determines the green threshold in the hue channel
      GREEN_RANGE = [65,170]/360;
      INTENSITY_T = 0.1;
      
      %converts to HSV color space
      hsv = rgb2hsv(rgbPatch);
      
      %generate a region of intereset (only areas which aren't black)
      relevanceMask = rgb2gray(rgbPatch)>0;
      
      %finds pixels within the specified range in the H and V channels
      greenAreasMask = hsv(:,:,1)>GREEN_RANGE(1) & hsv(:,:,1) < GREEN_RANGE(2) & hsv(:,:,3) > INTENSITY_T;
      
      %returns the mean in thie relevance mask
      res = sum(greenAreasMask(:)) / sum(relevanceMask(:));
      
      
      
      end
      

      <强>结果

      在绿色补丁上使用时:

      enter image description here enter image description here enter image description here

      greenPatch1 = imread('g1.PNG');
      evaluateLevelOfGreen(greenPatch1)
      greenPatch2 = imread('g2.PNG');
      evaluateLevelOfGreen(greenPatch2)
      greenPatch3 = imread('g3.PNG');
      evaluateLevelOfGreen(greenPatch3)
      

      结果:

      ans = 0.8230
      ans =  0.8340
      ans = 0.6030
      

      在非绿色补丁上使用时:

      enter image description here

      nonGreenPatch1 = imread('ng1.PNG');
      evaluateLevelOfGreen(nonGreenPatch1)
      

      结果:

      ans = 0.0197