获得BMP平均色彩 [英] get average color from bmp

查看:225
本文介绍了获得BMP平均色彩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的第2个屏幕的任务栏(类似displayfusion)。



不过,我有在获得从图标右侧平均颜色困难。例如谷歌浏览器/当我徘徊在它的主要任务栏上它的背景将变为黄色。随着我的代码,它变成橙/红



这是它现在看起来:





我怎样才能得到正确的显性/平均?彩



我使用此代码来计算平均色彩:

 公共静态颜色getDominantColor(BMP位图)
{
//用于理货
INT R = 0;
INT G = 0;
INT B = 0;

INT总= 0;

的for(int x = 0; X< bmp.Width; X ++)
{
为(INT Y = 0; Y< bmp.Height; Y ++)
{
色彩CLR = bmp.GetPixel(X,Y);
R + = clr.R;
G + = clr.G;
B + = CLR.B;
总++;
}
}

//计算平均
R / =总;
克/ =总;
B / =总;

返回Color.FromArgb(R,G,B);
}


解决方案

的平均颜色不neccessarily颜色最常用的。我建议计算具有超过一定阈值的饱和像素的色调,和使用数组来创建图像的直方图。 (有多少次用一定的色调值)。



然后将直方图平滑化(计算与两个邻国当地的平均值),然后得到的地方这种平滑直方图需要。最大值



您可以HSL值有:

  Color.GetHue 
Color.GetSaturation
Color.GetBrightness


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:

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);
}

解决方案

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

这篇关于获得BMP平均色彩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆