Reputation: 3081
I have a problem and I can't figure it out.
I have to use two different image processing techniques: Skelethonization and Thinning , and I have to do this in java. Now the problem is that I can't find out any starting point or tutorial on this. Can anybody tell me where should I start, or can somebody explain me how can I achieve that? I'm writing the application in Java, and I want to use a BufferedImage
, if it is possible (of course).
Thanks
Upvotes: 0
Views: 1691
Reputation: 557
You can draw to the BufferedImage like this:
public BufferedImage createSkelethonizationImage() {
BufferedImage image = new BufferedImage(width, height);
Graphics2D g2 = image.createGraphics();
// Perform your drawing here
g2.drawLine(...);
g2.dispose();
return image;
}
To draw the image, create a new class that extends JComponent and overrides the paint method. Here is some code to get started:
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);
}
}
Edit - Complete solution:
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);
}
}
Upvotes: 5