4

我试图在 C# 中将图像量化为 10 种颜色,但在绘制量化图像时遇到问题,我制作了映射表,它是正确的,我制作了原始图像的副本,我正在更改颜色基于映射表的像素,我使用以下代码:

bm = new Bitmap(pictureBox1.Image);
        Dictionary<Color, int> histo = new Dictionary<Color, int>();
        for (int x = 0; x < bm.Size.Width; x++)
            for (int y = 0; y < bm.Size.Height; y++)
            {
                Color c = bm.GetPixel(x, y);
                if (histo.ContainsKey(c))
                    histo[c] = histo[c] + 1;
                else
                    histo.Add(c, 1);
            }
        var result1 = histo.OrderByDescending(a => a.Value);
                  int ind = 0;
        List<Color> mostusedcolor = new List<Color>();
        foreach (var entry in result1)
        {
            if (ind < 10)
            {
                mostusedcolor.Add(entry.Key);
                ind++;
            }
            else
                break;
        }
        Double temp_red,temp_green,temp_blue,temp;
        Dictionary<Color, Double> dist = new Dictionary<Color, double>();
        Dictionary<Color, Color> mapping = new Dictionary<Color, Color>();
        foreach (var p in result1)
        {
            dist.Clear();
            foreach (Color pp in mostusedcolor)
            {
                temp_red = Math.Pow((Convert.ToDouble(p.Key.R) - Convert.ToDouble(pp.R)), 2.0);
                temp_green = Math.Pow((Convert.ToDouble(p.Key.G) - Convert.ToDouble(pp.G)), 2.0);
                temp_blue = Math.Pow((Convert.ToDouble(p.Key.B) - Convert.ToDouble(pp.B)), 2.0);
                temp = Math.Sqrt((temp_red + temp_green + temp_blue));
                dist.Add(pp, temp);
            }
            var min = dist.OrderBy(k=>k.Value).FirstOrDefault();
            mapping.Add(p.Key, min.Key);
        }
  Bitmap copy = new Bitmap(bm);

        for (int x = 0; x < copy.Size.Width; x++)
            for (int y = 0; y < copy.Size.Height; y++)
            {
                Color c = copy.GetPixel(x, y);
                Boolean flag = false;
                foreach (var entry3 in mapping)
                {
                    if (c.R == entry3.Key.R && c.G == entry3.Key.G && c.B == entry3.Key.B)
                    {
                        copy.SetPixel(x, y, entry3.Value);
                        flag = true;
                    }
                    if (flag == true)
                        break;

                }
            }
pictureBox2.Image=copy;
4

1 回答 1

5

您的代码有两个问题:

  • 它非常慢
  • 量化不是我所期望的。

这是原始图像,您的代码的结果以及 Photoshop 在要求减少到 10 种颜色时所做的操作:

在此处输入图像描述

  • 加速代码可以分两步完成:

    • 摆脱最讨厌的浪费时间
    • GetPixelSetPixel循环变成Lockbits循环。

这是第一步的解决方案,它将代码加速至少 100 倍:

Bitmap bm = (Bitmap)Bitmap.FromFile("d:\\ImgA_VGA.png");
pictureBox1.Image = bm;

Dictionary<Color, int> histo = new Dictionary<Color, int>();
for (int x = 0; x < bm.Size.Width; x++)
    for (int y = 0; y < bm.Size.Height; y++)
    {
        Color c = bm.GetPixel(x, y);   // **1**
        if (histo.ContainsKey(c))  histo[c] = histo[c] + 1;
        else histo.Add(c, 1);
    }
var result1 = histo.OrderByDescending(a => a.Value);
int number = 10;
var mostusedcolor = result1.Select(x => x.Key).Take(number).ToList();

Double temp;
Dictionary<Color, Double> dist = new Dictionary<Color, double>();
Dictionary<Color, Color> mapping = new Dictionary<Color, Color>();
foreach (var p in result1)
{
    dist.Clear();
    foreach (Color pp in mostusedcolor)
    {
        temp = Math.Abs(p.Key.R - pp.R) + 
               Math.Abs(p.Key.R - pp.R) + 
               Math.Abs(p.Key.R - pp.R);
        dist.Add(pp, temp);
    }
    var min = dist.OrderBy(k => k.Value).FirstOrDefault();
    mapping.Add(p.Key, min.Key);
}
Bitmap copy = new Bitmap(bm);

for (int x = 0; x < copy.Size.Width; x++)
    for (int y = 0; y < copy.Size.Height; y++)
    {
        Color c = copy.GetPixel(x, y);   // **2**
        copy.SetPixel(x, y, mapping[c]);
    }
pictureBox2.Image = copy;

请注意,如果我们只想订购颜色,则无需使用毕达哥拉斯的全部力量来计算距离。曼哈顿距离会很好。

另请注意,我们已经有了查找字典mapping,其中包含图像中的每种颜色作为其键,因此我们可以直接访问这些值。(这是迄今为止最糟糕的时间浪费..)

测试图像在~1s内处理,所以我什至不去LockBits修改..

  • 但是校正量化并不是那么简单,恐怕而且 imo 超出了一个好的 SO 问题的范围。

    • 但是让我们看看出了什么问题: 看一下结果,我们一眼就能看出:有很多天空,所有那么多蓝色像素都有超过 10 种色调,所以所有颜色都在前 10 位列表是蓝色的。

    • 所以整个图像没有其他色调了!

    • 要解决这个问题,您最好研究常见的量化算法..

修复代码的一种简单方法是丢弃/映射最常用列表中与您已经拥有的任何一种颜色太接近的所有颜色。但是找到最佳的最小距离需要体细胞数据分析。

更新另一种改进代码的非常简单的方法是用一些低位掩盖真实颜色,以将相似的颜色映射在一起。仅选择 10 种颜色仍然太少,但改进非常明显,即使对于此测试图像:

Color cutOff(Color c, byte mask)
{  return Color.FromArgb(255, c.R & mask, c.G & mask, c.B & mask );   }

在此处插入 ( 1 ):

byte mask = (byte)255 << 5 & 0xff;  // values of 3-5 worked best
Color c = cutOff(bm.GetPixel(x, y), mask);

在这里(2):

Color c = cutOff(copy.GetPixel(x, y), mask);

我们得到:

在此处输入图像描述

仍然缺少所有黄色、橙色或棕色的色调,但只增加了一条线,这是一个很好的改进。

于 2016-01-01T21:46:50.413 回答