0

我有一个问题,我无法弄清楚。我必须使用两种不同的图像处理技术:骨架化 和细化,我必须在 java 中做到这一点。现在的问题是我找不到任何起点或教程。谁能告诉我应该从哪里开始,或者有人可以解释我如何才能做到这一点?BufferedImage我正在用 Java 编写应用程序,如果可能的话,我想使用 a (当然)。

谢谢

4

1 回答 1

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);
    }
}
于 2012-04-24T20:51:18.473 回答