Java中的Skelethonization和Thinning

时间:2012-04-24 20:31:18

标签: java image image-processing

我遇到了问题,我无法理解。 我必须使用两种不同的图像处理技术:SkelethonizationThinning,我必须在java中执行此操作。现在的问题是我无法找到任何起点或教程。任何人都可以告诉我应该从哪里开始,或者有人可以解释我如何实现这一目标?我正在用Java编写应用程序,如果可能的话,我想使用BufferedImage。(当然)。

由于

1 个答案:

答案 0 :(得分:5)

您可以像这样绘制到BufferedImage:

public BufferedImage createSkelethonizationImage() {
   BufferedImage image = new BufferedImage(width, height);
   Graphics2D g2 = image.createGraphics();

   // Perform your drawing here
   g2.drawLine(...);

   g2.dispose();
   return image;
}

要绘制图像,请创建一个扩展JComponent的新类并覆盖paint方法。以下是一些入门代码:

public class MyImage extends JComponent {
   // Note: the image should be modified on the Event Dispatch Thread
   private BufferedImage image = createSkelethonizationImage();

   @Override
   public void paint(Graphics g) {
      g.drawImage(image, 0, 0, this);
   }
}

编辑 - 完整的解决方案:

public class Test {

    public static void main(String[] args) {
       // Width and height of your image
       final int width = 200;
       final int height = 200;

       SwingUtilities.invokeLater(new Runnable() {

          @Override
          public void run() {
             JFrame frame = new JFrame();
             MyImage image = new MyImage(width, height);
             frame.add(image);
             frame.setSize(new Dimension(width, height));
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);            
          }
       });
    }
}

class MyImage extends JComponent {

    // Note: image should be modified on the Event Dispatch Thread only
    private final BufferedImage image;

    public MyImage(int width, int height) {
       image = createSkelethonizationImage(width, height);
       setPreferredSize(new Dimension(width, height));
    }

    public BufferedImage createSkelethonizationImage(int width, int height) {
       BufferedImage iamge = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
       Graphics2D g2 = iamge.createGraphics();

       // Perform your drawing here
       g2.setColor(Color.BLACK);
       g2.drawLine(0, 0, 200, 200);

       g2.dispose();
       return iamge;
    }

    @Override
    public void paint(Graphics g) {
       g.drawImage(image, 0, 0, this);
    }
}