Skip to content

Instantly share code, notes, and snippets.

@tomgibara
Created October 18, 2011 16:21
Show Gist options
  • Save tomgibara/1295860 to your computer and use it in GitHub Desktop.
Save tomgibara/1295860 to your computer and use it in GitHub Desktop.
Canny Edge Detection Test
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestCanny {
public static void main(String[] args) throws IOException {
BufferedImage imageIn = ImageIO.read(new File("sourceImage.jpg"));
showImage("IN", imageIn);
CannyEdgeDetector canny = new CannyEdgeDetector();
canny.setSourceImage(imageIn);
canny.process();
BufferedImage imageOut = canny.getEdgesImage();
showImage("OUT", imageOut);
}
public static JFrame showImage(String title, BufferedImage image) {
final JFrame f = new JFrame(title);
Container pane = f.getContentPane();
JButton display = new JButton(new ImageIcon(image));
display.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.dispose();
}
});
display.setMargin(new Insets(50, 50, 50, 50));
pane.add(display);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
return f;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment