Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get average color from bmp

Tags:

c#

rgb

I am developing a taskbar for the 2nd screen(something like displayfusion).

However, I'm having difficulty at getting the right average color from the icon. For example Google Chrome/ When I hover it on the main taskbar it backgrounds turns yellow. With my code it turns orange/red.

This is what it looks now:

enter image description here

How can I get the right dominant/average color?

I use this code to calculate the average color:

public static Color getDominantColor(Bitmap bmp)
{
     //Used for tally
     int r = 0;
     int g = 0;
     int b = 0;

     int total = 0;

     for (int x = 0; x < bmp.Width; x++)
     {
          for (int y = 0; y < bmp.Height; y++)
          {
               Color clr = bmp.GetPixel(x, y);    
               r += clr.R;
               g += clr.G;
               b += clr.B;    
               total++;
          }
     }

     //Calculate average
     r /= total;
     g /= total;
     b /= total;

     return Color.FromArgb(r, g, b);
}
like image 982
Jelle Vergeer Avatar asked Mar 05 '11 16:03

Jelle Vergeer


People also ask

How do you calculate your average color?

The typical approach to averaging RGB colors is to add up all the red, green, and blue values, and divide each by the number of pixels to get the components of the final color.

What is the average colour?

The average colour is the sum of all pixels divided by the number of pixels. However, this approach may yield a colour different to the most prominent visual color. What you might really want is dominant color rather than average colour.

How do you find the average color in Photoshop?

5) Finding the Average ColorFilter > Blur > Average finds the average of all of the colors in an image (or in a selection) and fills the entire image (or selection) with that color.

How do I find the average image RGB value in Python?

Use the average() Function of NumPy to Find the Average Color of Images in Python. In mathematics, we can find the average of a vector by dividing the sum of all the elements in the vector by the total number of elements.


1 Answers

The average color is not neccessarily the color most used. I recommend calculating the HUE of pixels which have saturation over a certain threshold, and use an array to create a histogram of the image. (How many times a certain hue value was used).

Then smooth the histogram (calculate local average values with both neighbours), then get the place where this smoothed histogram takes the maximal value.

You can get HSL values with:

Color.GetHue
Color.GetSaturation
Color.GetBrightness
like image 54
vbence Avatar answered Oct 18 '22 07:10

vbence