c# – 获取图像中每种颜色的百分比使用情况
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 获取图像中每种颜色的百分比使用情况,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3427字,纯文字阅读大概需要5分钟。
内容图文
![c# – 获取图像中每种颜色的百分比使用情况](/upload/InfoBanner/zyjiaocheng/765/aca05042a07443689d2508e4e488bcdf.jpg)
我有这个工作,但它在jpeg图像上非常缓慢,也需要一些改变.
我需要知道图像中的各个颜色(RGB的公差为/ – 1)和该颜色的图像的百分比.
因此,如果图像是黑白的,它会说出类似的东西
白色:74%
黑色:26%
下面的代码就像我说的那样,但我需要添加一个容差系统,我不知道如何做到这一点.
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;
}
而另一个功能
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实际上并不是访问图像数据的快捷方式.使用LockBits方法.
编辑:
那么你用字符串做了很多事情.以这种方式构建pixelData Dictionary是没有用的,为什么不立即处理不同的颜色? Color是一个不可变的结构,所以这对我们的字典来说已经很好了.
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);
}
应该这样做,除非你想让它更快,并使用LockBits而不是GetPixel.
其他一些观察:
int hmm = (image.Height * image.Width);
double offset = 100 / double.Parse(hmm.ToString());
你正在使用一种非常奇怪且缓慢的从int转换为double的方法.你可以写双偏移= 100 /(双)hmm;并且它是相同的(你也可以写100.0而不是100,编译器将为你创建一个双,所以你不需要强制转换hmm).
这让我发笑:
IEnumerable<string> query = pixelData.Values.Where(fruit => fruit == value);
为何结果!?好像你从某个地方复制了这个.
内容总结
以上是互联网集市为您收集整理的c# – 获取图像中每种颜色的百分比使用情况全部内容,希望文章能够帮你解决c# – 获取图像中每种颜色的百分比使用情况所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。