Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
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
Comments
dan!sh 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.

1 solution

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);
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900