14,451,808 members
Rate this:
See more:
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?
Posted
lw@zi 21-Apr-11 23:29pm

Do you need to find just the red pixels or the area of red parts in the image? If it is area, is it regular shape or anything?
Sandeep Mewara 22-Apr-11 0:31am

Quite a few information is missing based on which members can reply more appropriately. Kindly update the question with the details.
Tom Deketelaere 22-Apr-11 8:10am

I believe Christian Graus has written more than a few articles for image manipulation so you might want to check those out and see if you can find what you need.

http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=6556

Other than that you'll need to give us more information in order for us to be able to help you.

Rate this:

## Solution 1

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);```
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Maciej Los 285 phil.o 233 Richard MacCutchan 228 Richard Deeming 170 OriginalGriff 85
 OriginalGriff 4,219 phil.o 3,158 Maciej Los 2,623 Richard Deeming 2,183 Richard MacCutchan 2,090

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100