反向(移除)抗锯齿滤波器

Fra*_*ard 5 graphics rendering image-processing antialiasing imagefilter

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

我正在使用GDI +,但我对代码不太感兴趣.如果可以构建这样的矩阵,我需要一个算法,可能是卷积滤波器.

灰度图像(应该)仅包含6种颜色(或不同的灰色阴影).这样我以后可以使用Color-Lookup过滤器对它们进行重新着色.但是,当保存的图像时,Photoshop会自动应用抗锯齿,因此边缘模糊(因为启用了双立方插值模式).我需要恢复这种效果.

这是一个例子:

在此输入图像描述

这是Photoshop的截图

有人建议我应该使用Sharpen过滤器,所以我在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);
    }
Run Code Online (Sandbox Code Playgroud)

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

消除锯齿的结果:http: //i.imgur.com/w6gELWJ.png

Roy*_*oyi 3

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

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

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

假设滤波器是某种 LPF,穷人的解决方案是某种高通滤波器 (HPF)。其中任何一个都会给人一种“更清晰的图像”和“增强的边缘”的外观。这种类型的已知过滤器是USM锐化:

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

享受...