有色区域面积 [英] Area of colored region
本文介绍了有色区域面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我的图像中有些区域是红色的.
我想计算所有这些区域的面积(或像素).
有人可以帮我吗?
Hello,
I have an image in which there is some regions are red.
I want to calculate area (or pixel) of total these regions.
Can anybody help me?
推荐答案
下面的代码会将图像转换为位图,并返回色相范围内的像素数.色相的值以度为单位,并且必须介于0.0到360.0之间.此方法支持使用比最大值更高的最小值,因此您可以将范围包裹在零度附近.您还必须在0到1.0之间指定可接受的饱和度值范围.
这样一来,您就可以选择模糊庄园中的所有像素,而不仅仅是一个RGB值.
The code below will convert an image to a bitmap and return the count of pixels that fall within a range of hues. The value of the hues are in degrees and must fall between 0.0 and 360.0. This method supports using a higher minimum value than maximum so you can wrap the range around zero degrees. You must also specify a range of acceptible saturation values between 0 and 1.0.
This allows you to select all pixels in a fuzzy manor instead of just one RGB value.
int GetPixelsForHueRange(Image image, float lowerHue, float upperHue, float minSaturation, float maxSaturation)
{
Bitmap bmp = new Bitmap(image);
//Handle wrap around
if (lowerHue > upperHue)
{
return GetPixelsForHueRange(image, lowerHue, 360.0f, minSaturation, maxSaturation) +
GetPixelsForHueRange(image, 0.0f, upperHue, minSaturation, maxSaturation);
}
int count = 0;
for (int i = 0; i < bmp.Size.Width; i++)
for (int j = 0; j < bmp.Size.Height; j++)
{
System.Drawing.Color c = bmp.GetPixel(i, j);
float hue = c.GetHue();
float sat = c.GetSaturation();
if ((hue >= lowerHue && hue <= upperHue) &&
(sat >=minSaturation && sat <= maxSaturation))
{
count++;
}
}
return count;
}
用法:
Usage:
Image img = Image.FromFile(@"C:\Users\me\Pictures\rainbow.jpg");
//Check for "red" pixels
int pixelCount = GetPixelsForHueRange(img, 350.0f, 10.0f, 0.30f,1.0f);
这篇关于有色区域面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文