【问题标题】:Reverse (remove) anti-aliasing filter反向(移除)抗混叠滤波器
【发布时间】:2014-07-23 23:40:18
【问题描述】:

我有一组抗锯齿灰度 PNG 图像。我需要知道如何以编程方式恢复抗锯齿效果并再次获得锐利的边缘。

我正在使用 GDI+,但我对代码不太感兴趣。我需要一个算法。

灰度图像(应该)仅包含 6 种颜色(或不同深浅的灰色)。这样以后我可以使用颜色查找过滤器重新着色它们。但是,当保存图像时,Photoshop 会自动应用抗锯齿,因此边缘会变得模糊(因为启用了双三次插值模式)。我需要恢复这种效果。

这是一个例子:

这是 Photoshop 的截图

有人建议我应该应用锐化滤镜,所以我在 Photoshop 上尝试了它。这是它的外观:

尽管外边缘很好,但 2 种不同颜色相遇的边缘会显示伪影。

编辑:

这就是我最终做到的方式。这是非常即兴的,可能可以做得更好更快,但我找不到更好的解决方案。

这个想法是遍历每个像素,获取其直接邻居并将其颜色与它们的颜色进行比较。如果它由至少 2 个相同颜色的像素支持,它会检查相邻像素是否也支持。如果不是,它会用自己的像素替换相邻像素。

代码:

    private static void Resample(Bitmap bmp)
    {
        // First we look for the most prominent colors
        // i.e. They make up at least 1% of the image
        Hashtable stats = new Hashtable();

        for (int x = 0; x < bmp.Width; x++)
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                Color px = bmp.GetPixel(x, y);
                if (px.A == 0)
                    continue;

                Color pxS = Color.FromArgb(255, px);
                if (stats.ContainsKey(pxS.ToArgb()))
                    stats[pxS.ToArgb()] = (int)stats[pxS.ToArgb()] + 1;
                else
                    stats.Add(pxS.ToArgb(), 1);
            }
        }

        float totalSize = bmp.Width*bmp.Height;
        float minAccepted = 0.01f;
        List<int> selectedColors = new List<int>();

        // Make up a list with the selected colors
        foreach (int key in stats.Keys)
        {
            int total = (int)stats[key];
            if (((float)total / totalSize) > minAccepted)
                selectedColors.Add(key);
        }

        // Keep growing the zones with the selected colors to cover the invalid colors created by the anti-aliasing
        while (GrowSelected(bmp, selectedColors));
    }

    private static bool GrowSelected(Bitmap bmp, List<int> selectedColors)
    {
        bool flag = false;

        for (int x = 0; x < bmp.Width; x++)
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                Color px = bmp.GetPixel(x, y);
                if (px.A == 0)
                    continue;

                Color pxS = Color.FromArgb(255, px);

                if (selectedColors.Contains(pxS.ToArgb()))
                {
                    if (!isBackedByNeighbors(bmp, x, y))
                        continue;

                    List<Point> neighbors = GetNeighbors(bmp, x, y);
                    foreach(Point p in neighbors)
                    {
                        Color n = bmp.GetPixel(p.X, p.Y);
                        if (!isBackedByNeighbors(bmp, p.X, p.Y))
                            bmp.SetPixel(p.X, p.Y, Color.FromArgb(n.A, pxS));
                    }
                }
                else
                {
                    flag = true;
                }
            }
        }

        return flag;
    }

    private static List<Point> GetNeighbors(Bitmap bmp, int x, int y)
    {
        List<Point> neighbors = new List<Point>();

        for (int i = x - 1; i > 0 && i <= x + 1 && i < bmp.Width; i++)
            for (int j = y - 1; j > 0 && j <= y + 1 && j < bmp.Height; j++)
                neighbors.Add(new Point(i, j));
        return neighbors;
    }

    private static bool isBackedByNeighbors(Bitmap bmp, int x, int y)
    {
        List<Point> neighbors = GetNeighbors(bmp, x, y);
        Color px = bmp.GetPixel(x, y);
        int similar = 0;
        foreach (Point p in neighbors)
        {
            Color n = bmp.GetPixel(p.X, p.Y);
            if (Color.FromArgb(255, px).ToArgb() == Color.FromArgb(255, n).ToArgb())
                similar++;
        }

        return (similar > 2);
    }

结果: 原图: http://i.imgur.com/8foQwFe.png

去锯齿结果: http://i.imgur.com/w6gELWJ.png

【问题讨论】:

  • 你需要提供一些你已经尝试过的例子,或者你遇到的问题。您不应该要求其他人为您完成任务。
  • 您可以通过扫描 3x3 块相同颜色的像素来获取要在输出中使用的颜色目录。

标签: image-processing graphics rendering antialiasing imagefilter


【解决方案1】:

滤波器的逆过程称为反卷积(这是一般逆问题的一种特殊情况)。
有两种类型的反卷积:

  1. 非盲反卷积 - 对图像的操作已知(例如,应用的低通滤波器的系数已知)。
  2. Blind Deconvolution - 在应用的滤波器具体未知的情况下,仅假设一些关于它的假设(例如 LPF 或空间不变等)。

这些通常是(其中任何一个)复杂的算法,需要时间(除非使用幼稚的“维纳滤波器”方法)。

假设滤波器是某种 LPF 穷人解决方案将是某种高通滤波器 (HPF)。 任何这些都会给人一种“更清晰的图像”和“增强的边缘”的感觉。 这种类型的已知滤镜是 Unsharp Mask:

  1. 在图像上应用 LPF(通常使用具有给定 STD 的高斯模糊)。我们称之为 lpfImage。
  2. 计算差异图像:diffImage = originalImage - lpfImage。
  3. “锐化蒙版图像”由以下公式给出:usmImage = originalImage + (alpha * diffImage)
    其中alpha 是“锐化”级别的预定义比例因子。

享受...

【讨论】:

  • 我怎样才能找出用于抗锯齿的 Photoshop 卷积矩阵是什么?它与双三次插值设置有关吗?您能否举例说明我们如何对抗混叠滤波器进行反卷积?还有,是模糊还是低通滤镜?
【解决方案2】:

我会试试Color Quantization

您需要构建某种类型的颜色直方图并从图像中获取最常用的颜色。可能是平滑之前的初始颜色。

创建一个包含前 6 种颜色的调色板。

将您的光栅 PNG 转换为索引 PNG。

我不确定 GDI+ 是否可以创建索引 PNG,但有很多 SDK 可以处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多