Pedro Marques
Pedro Marques

Reputation: 175

Matlab - How to detect green color on image?

I'm working in project that basically I have to detect the threes on image and delete the other information. I used HSV as segmentation and the function regionprops to detect each element. It works fine, but in same cases that has house roofs, they aren't deleted because the value of Hue is similar to the threes. So far, this is the result: enter image description here

To remove the roofs, I thought that maybe is possible detecting the color green in each region detected. If the region dont have 70% of green (for example) that region is deleted. How can I do that? How Can I detect only the green color of the image?

Upvotes: 4

Views: 2811

Answers (1)

ibezito
ibezito

Reputation: 5822

Solution Explanation

Evaluating the level of green in a patch is an interesting idea. I suggest the following approach:

  1. convert your patches from RGB to HSV color system. In the HSV color system it is easier to evaluate the hue (or - the color) of each pixel, by examining the first channel.

  2. Find the range for green color in the hue system. In our case it is about [65/360,170/360], as can be seen here:

enter image description here

  1. for each patch, calculate how many pixels have the hue value which is in the green range, and divide by the size of the connected component.

Code Expamle

The following function evaluate the "level of greenness" in a patch:

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

Results

When using on green patches:

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)

Results:

ans = 0.8230
ans =  0.8340
ans = 0.6030

when using on non green patches:

enter image description here

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

result:

ans = 0.0197

Upvotes: 5

Related Questions