This example has three steps: 1) read the input image; 2) image processing; 3) display the processed image.
For step 1, an image is read into image buffer.
File file = new File("Lenna.png"); BufferedImage image = null; // Read image try { image = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); } System.out.println("done");
Step 2 has a simple operation of image processing. It does 2D convolution filtering for the image using filt3x3 function in the ConvolutionMatrix class.
// 2D convolution for the image double[][] config = {{1,2,1}, {0,0,0}, {-1,-2,-1}}; ConvolutionMatrix imageConv = new ConvolutionMatrix(3); imageConv.applyConfig(config); image2 = imageConv.filt3x3(image, imageConv);
Step 3 displays the modified image
// Display the modified image ImageIcon icon=new ImageIcon(image); JFrame frame=new JFrame(); frame.setLayout(new FlowLayout()); frame.setSize(image.getWidth(),image.getHeight()); //Window.setSize(int width, int height) JLabel lbl=new JLabel(); lbl.setIcon(icon); frame.add(lbl); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Java-based image processing is used in many places. Hopefully some people can benefit from this tutorial. The code can be found from here
No comments:
Post a Comment