Matlab
Matlab - 如何检测图像上的绿色?(Matlab - How to detect green color on image?)我在项目中工作,基本上我必须在图像上检测三分并删除其他信息。 我使用HSV作为分段,并使用函数regionprops来检测每个元素。 它工作正常,但在具有屋顶的相同情况下,它们不会被删除,因为Hue的值类似于三个。 到目前为止,这是结果:
为了移除屋顶,我想也许可以检测到检测到的每个区域的绿色。 如果该区域没有70%的绿色(例如)该区域被删除。 我怎样才能做到这一点? 如何仅检测图像的绿色?
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:
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?
最满意答案解决方案说明
评估补丁中的绿色水平是一个有趣的想法。 我建议采用以下方法:
将补丁从RGB转换为HSV颜色系统。 在HSV颜色系统中,通过检查第一个通道,更容易评估每个像素的色调(或 - 颜色)。
在色调系统中找到绿色的范围。 在我们的例子中它大约是[65 / 360,170 / 360],如下所示:
对于每个补丁,计算有多少像素具有绿色范围内的色调值,并除以连接组分的大小。
代码Expamle
以下函数评估补丁中的“绿色等级”:
function [ res ] = evaluateLevelOfGreen( rgbPatch )%EVALUATELEVELOFGREEN Summary of this function goes here% Detailed explanation goes here%determines the green threshold in the hue channelGREEN_RANGE = [65,170]/360;INTENSITY_T = 0.1;%converts to HSV color spacehsv = 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 channelsgreenAreasMask = hsv(:,:,1)>GREEN_RANGE(1) & hsv(:,:,1) < GREEN_RANGE(2) & hsv(:,:,3) > INTENSITY_T;%returns the mean in thie relevance maskres = sum(greenAreasMask(:)) / sum(relevanceMask(:));end结果
在绿色补丁上使用时:
greenPatch1 = imread('g1.PNG');evaluateLevelOfGreen(greenPatch1)greenPatch2 = imread('g2.PNG');evaluateLevelOfGreen(greenPatch2)greenPatch3 = imread('g3.PNG');evaluateLevelOfGreen(greenPatch3)
结果:
ans = 0.8230ans = 0.8340ans = 0.6030在非绿色补丁上使用时:
nonGreenPatch1 = imread('ng1.PNG');evaluateLevelOfGreen(nonGreenPatch1)
结果:
ans = 0.0197Solution Explanation
Evaluating the level of green in a patch is an interesting idea. I suggest the following approach:
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.
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:
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 channelGREEN_RANGE = [65,170]/360;INTENSITY_T = 0.1;%converts to HSV color spacehsv = 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 channelsgreenAreasMask = hsv(:,:,1)>GREEN_RANGE(1) & hsv(:,:,1) < GREEN_RANGE(2) & hsv(:,:,3) > INTENSITY_T;%returns the mean in thie relevance maskres = sum(greenAreasMask(:)) / sum(relevanceMask(:));endResults
When using on green patches:
greenPatch1 = imread('g1.PNG');evaluateLevelOfGreen(greenPatch1)greenPatch2 = imread('g2.PNG');evaluateLevelOfGreen(greenPatch2)greenPatch3 = imread('g3.PNG');evaluateLevelOfGreen(greenPatch3)
Results:
ans = 0.8230ans = 0.8340ans = 0.6030when using on non green patches:
nonGreenPatch1 = imread('ng1.PNG');evaluateLevelOfGreen(nonGreenPatch1)result:
ans = 0.0197