Neo*_*Neo 4 c# pixel colors bitmap image-processing
我有这个工作,但它在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;
}
Run Code Online (Sandbox Code Playgroud)
而另一个功能
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;
}
Run Code Online (Sandbox Code Playgroud)
And*_*eas 11
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);
}
Run Code Online (Sandbox Code Playgroud)
应该这样做,除非你想让它更快,并使用LockBits而不是GetPixel.
其他一些观察:
int hmm = (image.Height * image.Width);
double offset = 100 / double.Parse(hmm.ToString());
Run Code Online (Sandbox Code Playgroud)
你正在使用一种非常奇怪且缓慢的从int转换为double的方法.你可以写double offset = 100 / (double)hmm;,它是相同的(你也可以写100.0而不是100,编译器将为你创建一个双,所以你不需要强制转换hmm).
这让我发笑:
IEnumerable<string> query = pixelData.Values.Where(fruit => fruit == value);
Run Code Online (Sandbox Code Playgroud)
为何结果!?好像你从某个地方复制了这个.
这似乎是更大的图像处理目标的一部分.该Aforge框架是去到了.NET中的图像分析和处理的选择,这是非常快的.它可能已经拥有您需要的代码.
你提到了一个公差系统 - 对我来说,这听起来像你需要量化 - 颜色四舍五入.
一旦有了量化的位图,就可以使用与调色板大小相匹配的长度和数组,LockBits位图,并使用每个像素的颜色索引作为每个像素的数组索引来累积使用统计信息.
您能否分享有关此代码目标的更多信息?该怎么做呢?