我遇到了一个问题,但我无法解决。 我必须使用两种不同的图像处理技术:Skelethonization和 Thinning ,我必须用java来做这个。现在的问题是我找不到任何关于此的起点或教程。谁能告诉我应该从哪里开始,或者有人可以解释我如何实现这一目标?我正在用 Java 编写应用程序,如果可能的话(当然),我想使用 BufferedImage。
谢谢
最佳答案
您可以像这样绘制 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);
}
}
关于java - java中的骨架化和细化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10305566/