1

I am writing a c# program and I am a noob in c#, although i am just fine in programming , i know C and C++. My program basically scans an image and locates the circle in that image and distinguishes them according to the coordinates of their centers. Now i want to make it find the brightness of the circle's color. i figured that it is enough to check the brightness of the center pixel, or even some pixels surrounding the circle too. But i couldn't do it so far. I tried using GetBrightness() in the color struct and get HUE but i couldn't specify what pixel i want it to work on. I hope i made myself clear and ask me for any more details. I will mention again that i am a noob in C# , all i know is C and C++

Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
user3609643
  • 15
  • 1
  • 4

1 Answers1

2

Take a look at this answer for the formula to calculate brightness from an RGB value: Formula to determine brightness of RGB color

In C# this would look something like:

public double GetBrightness(Color color)
{
    return (0.2126*color.R + 0.7152*color.G + 0.0722*color.B);
}

If you wanted to calculate the brightness of all colors in a circle, then you could do something like:

public double GetAverageBrightness(IEnumerable<Color> colors)
{
    int count = 0;
    double sumBrightness = 0;

    foreach (var color in colors)
    {
        count++;
        sumBrightness += GetBrightness(color);
    }

    return sumBrightness/count;        
}
Community
  • 1
  • 1
jt000
  • 3,196
  • 1
  • 18
  • 36
  • and how to use this method on a pixel I specify? let's say the pixel has the coordinates 50,100 – user3609643 Oct 07 '14 at 10:37
  • What class are you using to hold the pixels? If you're using the Bitmap class, then you'd use [GetPixel](http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel(v=vs.110).aspx) – jt000 Oct 07 '14 at 11:42
  • i have the X coordinates in an array and the Y coordinates in another array, it is easy to move from one to another. They are not stored in a class. Tell me what i should do, no need for thorough details i can look it up – user3609643 Oct 07 '14 at 11:54
  • Sounds to me like you haven't loaded the image. For this take a look at the [Bitmap class](http://msdn.microsoft.com/en-us/library/0cbhe98f(v=vs.110).aspx). Then you can call GetPixel on the X\Y coordinates that you have stored in your arrays. – jt000 Oct 07 '14 at 12:57
  • well i figured it out, the GetPixel method solved everything :D :D thank you bro @jaytre – user3609643 Oct 08 '14 at 07:23