Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package LetsStart; /** * * @author art */ import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.ButtonGroup; //import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.WindowConstants; import LetsStart.utils.ImageProcessor; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Scalar; import org.opencv.core.Size; //import org.opencv.core.Rect; //import org.opencv.core.Range; import org.opencv.imgproc.Imgproc; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; //import org.opencv.core.Point; public class GUI { private static final String blurString = "Blur"; private static final String gaussianString = "Gaussian"; private static final String medianString = "Median"; private static final String bilateralString = "Bilateral"; private static final String noneString = "None"; private JLabel imageView = new JLabel(); private JLabel imageView2 = new JLabel(); private String windowName; private Mat originalImage; private Mat image; //private Mat a; private Mat output; public Scalar color; private final ImageProcessor imageProcessor = new ImageProcessor(); protected String filterMode = noneString; public GUI(String windowName, Mat newImage) { super(); this.windowName = windowName; this.image = newImage; //this.a = newImage; //new Mat (100,100, CvType.CV_16UC3 ,new Scalar (new double[]{1,1,1})); originalImage = newImage.clone(); color = new Scalar(10, 10); processOperation(); updateView(); } public void init() { setSystemLookAndFeel(); initGUI(); } private void initGUI() { JFrame frame = createJFrame(windowName); updateView(); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } private JFrame createJFrame(String windowName) { JFrame frame = new JFrame(windowName); frame.setLayout(new GridBagLayout()); setupFilterRadio(frame); setupButtons(frame); setupImage(frame); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); return frame; } private void resetImage() { image = originalImage.clone(); filterMode = noneString; processOperation(); updateView(); } private void addNoise() { image = originalImage.clone(); Mat grayRnd = new Mat(image.rows(), image.cols(), image.type()); double noise = 128; grayRnd.setTo(new Scalar(noise / 2, noise / 2, noise / 2)); Core.subtract(image, grayRnd, image); Core.randu(grayRnd, 0, noise); Core.add(image, grayRnd, image); processOperation(); updateView(); } private void setupButtons(JFrame frame) { JButton noiseButton = new JButton("Add noise"); noiseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { addNoise(); } }); noiseButton.setAlignmentX(Component.CENTER_ALIGNMENT); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { resetImage(); } }); resetButton.setAlignmentX(Component.CENTER_ALIGNMENT); GridLayout gridRowLayout = new GridLayout(1, 0); JPanel buttonsPanel = new JPanel(gridRowLayout); buttonsPanel.add(resetButton); buttonsPanel.add(noiseButton); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 2; frame.add(buttonsPanel, c); } private void setupFilterRadio(JFrame frame) { JRadioButton noneButton = new JRadioButton(noneString); noneButton.setMnemonic(KeyEvent.VK_N); noneButton.setActionCommand(noneString); noneButton.setSelected(true); JRadioButton blurButton = new JRadioButton(blurString); blurButton.setMnemonic(KeyEvent.VK_B); blurButton.setActionCommand(blurString); JRadioButton gaussianButton = new JRadioButton(gaussianString); gaussianButton.setMnemonic(KeyEvent.VK_G); gaussianButton.setActionCommand(gaussianString); JRadioButton medianButton = new JRadioButton(medianString); medianButton.setMnemonic(KeyEvent.VK_M); medianButton.setActionCommand(medianString); JRadioButton bilateralButton = new JRadioButton(bilateralString); bilateralButton.setMnemonic(KeyEvent.VK_L); bilateralButton.setActionCommand(bilateralString); ButtonGroup group = new ButtonGroup(); group.add(noneButton); group.add(blurButton); group.add(gaussianButton); group.add(medianButton); group.add(bilateralButton); ActionListener operationChangeListener = new ActionListener() { public void actionPerformed(ActionEvent event) { filterMode = event.getActionCommand(); processOperation(); updateView(); } }; noneButton.addActionListener(operationChangeListener); blurButton.addActionListener(operationChangeListener); gaussianButton.addActionListener(operationChangeListener); medianButton.addActionListener(operationChangeListener); bilateralButton.addActionListener(operationChangeListener); GridLayout gridRowLayout = new GridLayout(1, 0); JPanel radioOperationPanel = new JPanel(gridRowLayout); JLabel modeLabel = new JLabel("Filter:", JLabel.RIGHT); radioOperationPanel.add(modeLabel); radioOperationPanel.add(noneButton); radioOperationPanel.add(blurButton); radioOperationPanel.add(bilateralButton); radioOperationPanel.add(gaussianButton); radioOperationPanel.add(medianButton); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; frame.add(radioOperationPanel, c); } private void setupImage(JFrame frame) { GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.CENTER; c.gridy = 3; c.gridx = 0; frame.add(imageView); frame.add(imageView2); imageView.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { color = new Scalar(image.get(e.getY(), e.getX())); //Imgproc.circle(image,new Point(e.getX(),e.getY()),20, new Scalar(0,0,255), 4); System.out.println("coords " + e.getX() + " " + e.getY()); System.out.println("the point is " + image.get(e.getY(), e.getX())[0] + " " + image.get(e.getY(), e.getX())[1] + " " + image.get(e.getY(), e.getX())[2]); //updateView(image); System.out.println(color.toString()); //a = new Mat (100,100, CvType.CV_16UC3 ,color); updateView(); } }); } private void setSystemLookAndFeel() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } } private void updateView() { Image outputImage = imageProcessor.toBufferedImage(output); imageView.setIcon(new ImageIcon(outputImage)); //Mat mat = new Mat (image,new Range(500,600), new Range(500,600));//new Rect(30,30,100,100));//new Mat(new Size(300,300), CvType.CV_8UC3, new Scalar (new double[]{128,3,4})); Mat a = new Mat(100, 100, CvType.CV_8UC3, color); Image outputImage2 = imageProcessor.toBufferedImage(a); imageView2.setIcon(new ImageIcon(outputImage2)); //double[] a= image.get(0, 0); //System.out.println("the value is " + a[0] +", " +a[1]+", "+a[2]); } private void processOperation() { if (noneString.equals(filterMode)) { output = image.clone(); } else { output = new Mat(image.rows(), image.cols(), image.type()); Size size = new Size(10.0, 10.0); if (blurString.equals(filterMode)) { Imgproc.blur(image, output, size); } else if (gaussianString.equals(filterMode)) { //Imgproc.GaussianBlur(image, output, size, 0); } else if (medianString.equals(filterMode)) { Imgproc.medianBlur(image, output, 3); } else if (bilateralString.equals(filterMode)) { Imgproc.bilateralFilter(image, output, 9, 100, 100); } } } }