获得每个颜色的使用百分比的图像中 [英] Get the percentage usage of every colour in an image
问题描述
我有这一个工作,但它是如此的JPEG图像该死的慢,也需要一定的变化。
我需要知道的图像中的各个颜色(具有+/- 1用于RGB的公差),那就是颜色。图像的%
因此,如果图像是黑白的它会这样说
白:74%
黑色:26%
下code ++工程,就像我说的,但我需要添加一个公差系统,以及和我有我会怎么做,没有想法。
私人字典<字符串,字符串> getPixelData(位图图像)
{
字典<字符串,字符串> PixelData取出=新词典<字符串,字符串>();
// INT关口,排;
// INT R,G,B;
彩色像素; 双胶纸= 0.000001;
INT嗯=(image.Height * image.Width);
双流= 0;
偏移量= 100 / double.Parse(hmm.ToString()); // 0.01; // 100 /(image.Height * image.Width)* 10000; 尝试
{
的for(int i = 0; I< image.Height;我++)
{
对于(INT J = 0; J< image.Width; J ++)
{
电流=电流+偏移;
像素= image.GetPixel(I,J);
pixelData.Add第(i +,+ J,(pixel.R.ToString()++ pixel.G.ToString()++ pixel.B.ToString()));
pBarprocess.Value = int.Parse(Math.Floor(电流)的ToString());
pBarprocess.Update();
Application.DoEvents();
}
}
}
赶上(异常前)
{
MessageBox.Show(无法解析图像+ EX);
} 返回PixelData取出;
}
和其它功能
私人无效btnProcess_Click(对象发件人,EventArgs的发送)
{
pBarprocess.Value = 0;
pBarprocess.Enabled = FALSE;
位图富= Bitmap.FromFile(@ txtFileName.Text)为位图;
字典<字符串,字符串> PixelData取出=新词典<字符串,字符串>(); lblProcess.Text =处理像素图;
PixelData取出= getPixelData(富); lblProcess.Text =计算密度;
lblProcess.Update(); 。变种distinctList = pixelData.Values.Distinct()了ToList(); Console.WriteLine(DL =+ distinctList.Count);
双偏置= 100 / double.Parse(distinctList.Count.ToString());
双流= 0; 的foreach(在distinctList VAR值)
{
IEnumerable的<串GT;查询= pixelData.Values.Where(水果=>水果==值);
双PERC =(double.Parse(query.Count()的ToString())/ double.Parse(pixelData.Count.ToString()))* 100;
Console.WriteLine(值+=+ query.Count()+(+ PERC +%));
txtAnalysis.Text =颜色+价值+:+ query.Count()+(+ perc.ToString()+%)\\ r \\ n+ txtAnalysis.Text;
txtAnalysis.Update();
pBarprocess.Value = int.Parse(Math.Floor(电流)的ToString());
pBarprocess.Update();
Application.DoEvents();
} lblProcess.Text =完了。
pBarprocess.Value = 0;
pBarprocess.Enabled = FALSE;
}
GetPixel是不是真的来访问图像数据的快速方式。使用 LockBits 方法。
编辑:
那么你做了很多事情用绳子。构建PixelData取出字典的方法是pretty没用的,你为什么不处理不同的颜色,对吗?色彩是一个不变的结构,所以这是我们已经字典一个良好的关键。
词典<颜色,诠释>频率=新词典<颜色,诠释>();
的for(int i = 0; I< image.Height;我++){
对于(INT J = 0; J< image.Width; J ++){
像素= image.GetPixel(I,J);
如果(frequency.ContainsKey(像素))的频率[像素] ++;
别的frequency.Add(象素,1);
}
}//最后
INT totalPixels = image.Width * image.Height;
的foreach(频率VAR KVP){
Console.WriteLine(色彩(R = {0},G = {1},B = {2}):{3},kvp.Key.R,kvp.Key.G,kvp.Key.B,KVP .value的/(双)totalPixels);
}
和应该这样做,当你想让它甚至更快,而不是使用GetPixel LockBits除外。
其他的一些意见:
INT嗯=(image.Height * image.Width);
双偏置= 100 / double.Parse(hmm.ToString());
您正在使用从int翻番铸造一个很奇怪的和缓慢的方式。你可以只写双胶纸= 100 /(双)嗯;
键,它是相同的(你也可以写100.0,而不是100和编译器将创建一个双为你你不需要投HMM)。
这让我笑了:
的IEnumerable<串GT;查询= pixelData.Values.Where(水果=>水果==值);
为什么水果!?好像你从什么地方复制这一点。
I have this one working but it is so damn slow on jpeg images and also needs some changing.
I need to know the individual colours in an image (with a tolerance of +/- 1 for RGB) and the % of the image that is that colour.
so if an image was black and white it would say something like White : 74% Black : 26%
The code below works like I said but I need to add a tolerance system as well and I have no idea on how I would do that.
private Dictionary<string, string> getPixelData(Bitmap image)
{
Dictionary<string, string> pixelData = new Dictionary<string, string>();
//int col, row;
//int r, g, b;
Color pixel;
double offset = 0.000001;
int hmm = (image.Height * image.Width);
double current = 0;
offset = 100 / double.Parse(hmm.ToString());// 0.01;// 100 / (image.Height * image.Width) * 10000;
try
{
for (int i = 0; i < image.Height; i++)
{
for (int j = 0; j < image.Width; j++)
{
current = current + offset;
pixel = image.GetPixel(i, j);
pixelData.Add(i + "," + j, (pixel.R.ToString() + " " + pixel.G.ToString() + " " + pixel.B.ToString()));
pBarprocess.Value = int.Parse(Math.Floor(current).ToString());
pBarprocess.Update();
Application.DoEvents();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Unable to parse image " + ex);
}
return pixelData;
}
And the other function
private void btnProcess_Click(object sender, EventArgs e)
{
pBarprocess.Value = 0;
pBarprocess.Enabled = false;
Bitmap foo = Bitmap.FromFile(@txtFileName.Text) as Bitmap;
Dictionary<string, string> pixelData = new Dictionary<string, string>();
lblProcess.Text = "Processing pixel map";
pixelData = getPixelData(foo);
lblProcess.Text = "Calculating Density";
lblProcess.Update();
var distinctList = pixelData.Values.Distinct().ToList();
Console.WriteLine("DL = " + distinctList.Count);
double offset = 100 / double.Parse(distinctList.Count.ToString());
double current = 0;
foreach (var value in distinctList)
{
IEnumerable<string> query = pixelData.Values.Where(fruit => fruit == value);
double perc = (double.Parse(query.Count().ToString()) / double.Parse(pixelData.Count.ToString())) * 100;
Console.WriteLine(value + " = " + query.Count() + "(" + perc + "%)");
txtAnalysis.Text = "Colour " + value + " : " + query.Count() + " (" + perc.ToString() + "%)\r\n" + txtAnalysis.Text;
txtAnalysis.Update();
pBarprocess.Value = int.Parse(Math.Floor(current).ToString());
pBarprocess.Update();
Application.DoEvents();
}
lblProcess.Text = "Finished.";
pBarprocess.Value = 0;
pBarprocess.Enabled = false;
}
GetPixel is not really a fast way to access image data. Use the LockBits method.
EDIT:
Well you're doing a lot of things with strings. Building the pixelData Dictionary that way is pretty useless, why don't you process the distinct colors right away? Color is an immutable struct, so that's a good key for our dictionary already.
Dictionary<Color, int> frequency = new Dictionary<Color, int>();
for (int i = 0; i < image.Height; i++) {
for (int j = 0; j < image.Width; j++) {
pixel = image.GetPixel(i, j);
if (frequency.ContainsKey(pixel)) frequency[pixel]++;
else frequency.Add(pixel, 1);
}
}
// and finally
int totalPixels = image.Width * image.Height;
foreach (var kvp in frequency) {
Console.WriteLine("Color (R={0},G={1},B={2}): {3}", kvp.Key.R, kvp.Key.G, kvp.Key.B, kvp.Value / (double)totalPixels);
}
And that should do it, except when you want to make it even faster and use LockBits instead of GetPixel.
Some other observations:
int hmm = (image.Height * image.Width);
double offset = 100 / double.Parse(hmm.ToString());
You're using a very strange and slow way of casting from int to double. You can just write double offset = 100 / (double)hmm;
and it's the same (you could also write 100.0 and not 100 and the compiler will create a double for you so you don't need to cast hmm).
This made me laugh:
IEnumerable<string> query = pixelData.Values.Where(fruit => fruit == value);
Why fruit!? Seems like you copied this from somewhere.
这篇关于获得每个颜色的使用百分比的图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!