【问题标题】:Smoothing a jagged path平滑锯齿状路径
【发布时间】:2011-11-05 07:23:04
【问题描述】:

前几天我参与了Image/Graphic into a Shape 的线程,并通过将Rectangle 迭代地添加到Area 来尝试获取图像的轮廓。那很慢。

此示例改为构建 GeneralPath 并从 GP 创建 Area。更快。

左上角的图像是“源图像”。右边两个是处理轮廓的各个阶段。它们在圆圈周围和三角形的斜边上都有锯齿状的边缘。

我想获得一种消除或减少锯齿状的形状。

在 ASCII 艺术中。

案例一:

  1234
1 **
2 **
3 ***
4 ***
5 ****
6 ****

转角在:

  • (2,3) 内角
  • (3,3)
  • (3,5) 内角
  • (4,5)

案例2:

  1234
1 ****
2 ****
3 **
4 **
5 ****
6 ****

转角在:

  • (4,2)
  • (2,2) 内角
  • (2,5) 内角
  • (4,5)

假设我们的路径具有显示的形状和列出的点,我想删除第一组的“内角”点,同时保留“一对”内角(图片中的一点) 第二次。


  • 谁能建议一些巧妙的内置方法来完成这项繁重的工作?
  • 如果做不到这一点,识别内角的位置和性质(一对/单个)的好方法是什么? (我猜我可以得到一个 PathIterator 并构建一个新的 GeneralPath 删除奇异的内角 - 只要我能弄清楚如何识别它们!)。

这是要玩的代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

/* Gain the outline of an image for further processing. */
class ImageOutline {

    private BufferedImage image;

    private TwoToneImageFilter twoToneFilter;
    private BufferedImage imageTwoTone;
    private JLabel labelTwoTone;

    private BufferedImage imageOutline;
    private Area areaOutline = null;
    private JLabel labelOutline;

    private JLabel targetColor;
    private JSlider tolerance;

    private JProgressBar progress;
    private SwingWorker sw;

    public ImageOutline(BufferedImage image) {
        this.image = image;
        imageTwoTone = new BufferedImage(
            image.getWidth(),
            image.getHeight(),
            BufferedImage.TYPE_INT_RGB);
    }

    public void drawOutline() {
        if (areaOutline!=null) {
            Graphics2D g = imageOutline.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,imageOutline.getWidth(),imageOutline.getHeight());

            g.setColor(Color.RED);
            g.setClip(areaOutline);
            g.fillRect(0,0,imageOutline.getWidth(),imageOutline.getHeight());
            g.setColor(Color.BLACK);
            g.setClip(null);
            g.draw(areaOutline);

            g.dispose();
        }
    }

    public Area getOutline(Color target, BufferedImage bi) {
        // construct the GeneralPath
        GeneralPath gp = new GeneralPath();

        boolean cont = false;
        int targetRGB = target.getRGB();
        for (int xx=0; xx<bi.getWidth(); xx++) {
            for (int yy=0; yy<bi.getHeight(); yy++) {
                if (bi.getRGB(xx,yy)==targetRGB) {
                    if (cont) {
                        gp.lineTo(xx,yy);
                        gp.lineTo(xx,yy+1);
                        gp.lineTo(xx+1,yy+1);
                        gp.lineTo(xx+1,yy);
                        gp.lineTo(xx,yy);
                    } else {
                        gp.moveTo(xx,yy);
                    }
                    cont = true;
                } else {
                    cont = false;
                }
            }
            cont = false;
        }
        gp.closePath();

        // construct the Area from the GP & return it
        return new Area(gp);
    }

    public JPanel getGui() {
        JPanel images = new JPanel(new GridLayout(2,2,2,2));
        JPanel  gui = new JPanel(new BorderLayout(3,3));

        JPanel originalImage =  new JPanel(new BorderLayout(2,2));
        final JLabel originalLabel = new JLabel(new ImageIcon(image));
        targetColor = new JLabel("Target Color");
        targetColor.setForeground(Color.RED);
        targetColor.setBackground(Color.WHITE);
        targetColor.setBorder(new LineBorder(Color.BLACK));
        targetColor.setOpaque(true);

        JPanel controls = new JPanel(new BorderLayout());
        controls.add(targetColor, BorderLayout.WEST);
        originalLabel.addMouseListener( new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent me) {
                originalLabel.setCursor(
                    Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent me) {
                originalLabel.setCursor(Cursor.getDefaultCursor());
            }

            @Override
            public void mouseClicked(MouseEvent me) {
                int x = me.getX();
                int y = me.getY();

                Color c = new Color( image.getRGB(x,y) );
                targetColor.setBackground( c );

                updateImages();
            }
        });
        originalImage.add(originalLabel);

        tolerance = new JSlider(
            JSlider.HORIZONTAL,
            0,
            255,
            104
            );
        tolerance.addChangeListener( new ChangeListener() {
            public void stateChanged(ChangeEvent ce) {
                updateImages();
            }
        });
        controls.add(tolerance, BorderLayout.CENTER);
        gui.add(controls,BorderLayout.NORTH);

        images.add(originalImage);

        labelTwoTone = new JLabel(new ImageIcon(imageTwoTone));

        images.add(labelTwoTone);

        images.add(new JLabel("Smoothed Outline"));

        imageOutline = new BufferedImage(
            image.getWidth(),
            image.getHeight(),
            BufferedImage.TYPE_INT_RGB
            );

        labelOutline = new JLabel(new ImageIcon(imageOutline));
        images.add(labelOutline);

        updateImages();

        progress = new JProgressBar();

        gui.add(images, BorderLayout.CENTER);
        gui.add(progress, BorderLayout.SOUTH);

        return gui;
    }

    private void updateImages() {
        if (sw!=null) {
            sw.cancel(true);
        }
        sw = new SwingWorker() {
            @Override
            public String doInBackground() {
                progress.setIndeterminate(true);
                adjustTwoToneImage();
                labelTwoTone.repaint();
                areaOutline = getOutline(Color.BLACK, imageTwoTone);

                drawOutline();

                return "";
            }

            @Override
            protected void done() {
                labelOutline.repaint();
                progress.setIndeterminate(false);
            }
        };
        sw.execute();
    }

    public void adjustTwoToneImage() {
        twoToneFilter = new TwoToneImageFilter(
            targetColor.getBackground(),
            tolerance.getValue());

        Graphics2D g = imageTwoTone.createGraphics();
        g.drawImage(image, twoToneFilter, 0, 0);

        g.dispose();
    }

    public static void main(String[] args) throws Exception {
        int size = 150;
        final BufferedImage outline =
            new BufferedImage(size,size,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = outline.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,size,size);
        g.setRenderingHint(
            RenderingHints.KEY_DITHERING,
            RenderingHints.VALUE_DITHER_ENABLE);
        g.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        Polygon p = new Polygon();
        p.addPoint(size/2, size/10);
        p.addPoint(size-10, size-10);
        p.addPoint(10, size-10);
        Area a = new Area(p);

        Rectangle r = new Rectangle(size/4, 8*size/10, size/2, 2*size/10);
        a.subtract(new Area(r));

        int radius = size/10;
        Ellipse2D.Double c = new Ellipse2D.Double(
            (size/2)-radius,
            (size/2)-radius,
            2*radius,
            2*radius
            );
        a.subtract(new Area(c));

        g.setColor(Color.BLACK);
        g.fill(a);

        ImageOutline io = new ImageOutline(outline);

        JFrame f = new JFrame("Image Outline");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(io.getGui());
        f.pack();
        f.setResizable(false);
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }
}

class TwoToneImageFilter implements BufferedImageOp {

    Color target;
    int tolerance;

    TwoToneImageFilter(Color target, int tolerance) {
        this.target = target;
        this.tolerance = tolerance;
    }

    private boolean isIncluded(Color pixel) {
        int rT = target.getRed();
        int gT = target.getGreen();
        int bT = target.getBlue();
        int rP = pixel.getRed();
        int gP = pixel.getGreen();
        int bP = pixel.getBlue();
        return(
            (rP-tolerance<=rT) && (rT<=rP+tolerance) &&
            (gP-tolerance<=gT) && (gT<=gP+tolerance) &&
            (bP-tolerance<=bT) && (bT<=bP+tolerance) );
    }

    public BufferedImage createCompatibleDestImage(
        BufferedImage src,
        ColorModel destCM) {
        BufferedImage bi = new BufferedImage(
            src.getWidth(),
            src.getHeight(),
            BufferedImage.TYPE_INT_RGB);
        return bi;
    }

    public BufferedImage filter(
        BufferedImage src,
        BufferedImage dest) {

        if (dest==null) {
            dest = createCompatibleDestImage(src, null);
        }

        for (int x=0; x<src.getWidth(); x++) {
            for (int y=0; y<src.getHeight(); y++) {
                Color pixel = new Color(src.getRGB(x,y));
                Color write = Color.BLACK;
                if (isIncluded(pixel)) {
                    write = Color.WHITE;
                }
                dest.setRGB(x,y,write.getRGB());
            }
        }

        return dest;
    }

    public Rectangle2D getBounds2D(BufferedImage src) {
        return new Rectangle2D.Double(0, 0, src.getWidth(), src.getHeight());
    }

    public Point2D getPoint2D(
        Point2D srcPt,
        Point2D dstPt) {
        // no co-ord translation
        return srcPt;
    }

    public RenderingHints getRenderingHints() {
        return null;
    }
}

【问题讨论】:

  • 关于一个难题的好问题。
  • @AndrewThompson 这可能是我使用它的方式,但似乎这不喜欢做直线段......我正在将此代码用于 JRPG 游戏(更多概念验证)和我正在使用区域的碰撞。绘制该区域时,它看起来像这样:puu.sh/7wJA2.png。如果我在该行下方添加一个像素,它看起来很好,就像这样:puu.sh/7wJGF.png。我对这段代码的理解相当薄弱,所以我不太确定在哪里问题在于。你有什么建议吗?
  • “你有什么建议吗?” 开始一个新问题,链接回这个问题。
  • @AndrewThompson 那是我想做的事情:) 我现在就这样做

标签: java image image-processing java-2d edge-detection


【解决方案1】:

如果您已经知道片段或边缘,请尝试使用高斯或平均值或您自己的内核之一进行模糊,然后移动到您想要平滑的边缘。 这是一个快速的解决方案,可能不适合复杂的图像,但对于自绘来说,它很好。

【讨论】:

    【解决方案2】:

    这个问题最普遍的版本是大多数计算机视觉管道的初始阶段之一。它被称为图像分割。它将图像分割成被认为在视觉上相同的像素区域。这些区域由“轮廓”分隔(参见例如this article),这相当于通过图像沿像素边界运行的路径。

    有一个简单的递归算法可以将轮廓表示为一条折线,这样定义的折线中的任何点都不会超过您可以选择的某个固定量(比如max_dev)。通常是 1/2 到 2 像素。

    function getPolyline(points [p0, p1, p2... pn] in a contour, max_dev) {
      if n <= 1 (there are only one or two pixels), return the whole contour
      Let pi, 0 <= i <= n, be the point farthest from the line segment p0<->pn
      if distance(pi, p0<->pn) < max_dev 
        return [ p0 -> pn ]
      else
        return concat(getPolyline [ p0, ..., pi ],  getPolyline [ pi, ..., pn] )
    

    这背后的想法是,您似乎有已经被分割的卡通图像。因此,如果您编写一个将边缘像素组装成链的简单搜索,您可以使用上面的算法将它们转换为平滑的线段链。它们甚至可以用抗锯齿绘制。

    【讨论】:

      【解决方案3】:

      这是一个很大的话题。您可能会发现 Depixelizing Pixel Art1 by Johannes Kopf & Dani Lischinski 很有用:它可读性强,是最新的,包括对以前工作的总结,并详细解释了他们的方法。

      另请参阅 slides covering similar backgroundvideo(!)

      1. 以下是“最近邻”与“他们的技术”文档中的一些屏幕截图。

      【讨论】:

      • 为了庆祝 PDF 的回归,我从文档中捕获并添加了一些“品尝者”图像。这远远超出了我的尝试,但仍然是一个令人印象深刻的结果。
      • 死链接 - 这就是为什么只有链接的答案不好。
      猜你喜欢
      • 2018-02-22
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多