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 cpsd; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import static java.lang.Math.pow; import java.util.ArrayList; import java.util.HashMap; import javax.imageio.ImageIO; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import static org.bytedeco.javacpp.opencv_imgproc.CV_CHAIN_APPROX_SIMPLE; import static org.bytedeco.javacpp.opencv_imgproc.CV_RETR_EXTERNAL; import static org.bytedeco.javacpp.opencv_imgproc.CV_THRESH_BINARY; /*import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.statistics.HistogramDataset; import org.jfree.data.statistics.HistogramType; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection;*/ import org.opencv.core.Core; import static org.opencv.core.Core.NORM_MINMAX; import static org.opencv.core.Core.normalize; import org.opencv.core.CvType; import static org.opencv.core.CvType.CV_8U; import static org.opencv.core.CvType.CV_8UC1; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfInt4; import org.opencv.core.MatOfPoint; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; import static org.opencv.imgproc.Imgproc.COLOR_RGB2GRAY; import static org.opencv.imgproc.Imgproc.CV_DIST_L2; import static org.opencv.imgproc.Imgproc.INTER_CUBIC; import static org.opencv.imgproc.Imgproc.contourArea; import static org.opencv.imgproc.Imgproc.distanceTransform; import static org.opencv.imgproc.Imgproc.threshold; /** * * @author root */ public class ImageGUI extends javax.swing.JFrame { public ImageClass image; DefaultListModel listModel; String[] str = new String[7]; String radio = new String(); String[] transforms = { "Prewitt Transform", "Sobel Transform", "Kirsch Transform", "Robinson Transform", "Laplace Transform" }; int[][] prewitt = { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } }; int[][] sobel = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; int[][] kirsch = { { -3, -3, -3 }, { -3, 0, -3 }, { 5, 5, 5 } }; int[][] robinson = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; int[][] laplacian = { { 0, -1, 0 }, { -1, 4, -1 }, { 0, -1, 0 } }; int[][][] transformArray = { prewitt, sobel, kirsch, robinson, laplacian }; HashMap<String, int[][]> hm = new HashMap<String, int[][]>(); //KMeans kmeans = new KMeans(); double sensorSize = 0; int resolution1 = 0; int resolution2 = 0; double magnification = 1; double objectDistance = 0; FileInputStream in = null; BufferedReader br = null; /** * Creates new form ImageGUI */ public ImageGUI() { initComponents(); try { in = new FileInputStream("/home/admin/Downloads/CoalImages/cameraData.txt"); br = new BufferedReader(new InputStreamReader(in)); sensorSize = Double.parseDouble(br.readLine()); resolution1 = Integer.parseInt(br.readLine()); resolution2 = Integer.parseInt(br.readLine()); magnification = Double.parseDouble(br.readLine()); objectDistance = Double.parseDouble(br.readLine()); br.close(); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } buttonGroup1.add(LaplaceButton); buttonGroup1.add(SobelButton); buttonGroup1.add(PrewittButton); buttonGroup1.add(KirschButton); buttonGroup1.add(RobinsonButton); for (int k = 0; k < 7; k++) { str[k] = "default"; } for (int k = 0; k < transforms.length; k++) { //System.out.println(transformArray[k][0][0]); hm.put(transforms[k], transformArray[k]); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jTabbedPane1 = new javax.swing.JTabbedPane(); fileChooser = new javax.swing.JFileChooser(); buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); jSplitPane1 = new javax.swing.JSplitPane(); jSplitPane2 = new javax.swing.JSplitPane(); originalPane = new javax.swing.JScrollPane(); processedPane = new javax.swing.JScrollPane(); jPanel1 = new javax.swing.JPanel(); operationstab = new javax.swing.JTabbedPane(); jPanel2 = new javax.swing.JPanel(); pathLabel = new javax.swing.JLabel(); browse = new javax.swing.JButton(); path = new javax.swing.JTextField(); customizeCameraButton = new javax.swing.JButton(); jPanel3 = new javax.swing.JPanel(); jComboBox1 = new javax.swing.JComboBox(); jComboBox2 = new javax.swing.JComboBox(); jComboBox3 = new javax.swing.JComboBox(); jComboBox4 = new javax.swing.JComboBox(); jComboBox5 = new javax.swing.JComboBox(); process = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); zoomSlider = new javax.swing.JSlider(); sharpSlider = new javax.swing.JSlider(); jLabel2 = new javax.swing.JLabel(); reset = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); LaplaceButton = new javax.swing.JRadioButton(); SobelButton = new javax.swing.JRadioButton(); ApplyTransformButton = new javax.swing.JButton(); PrewittButton = new javax.swing.JRadioButton(); KirschButton = new javax.swing.JRadioButton(); RobinsonButton = new javax.swing.JRadioButton(); AnalyzeButton = new javax.swing.JButton(); cannyButton = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jPanel5 = new javax.swing.JPanel(); segmentButton = new javax.swing.JButton(); ratioButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jSplitPane1.setDividerLocation(150); jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); jSplitPane1.setEnabled(false); jSplitPane2.setDividerLocation(550); originalPane.setToolTipText("Original Image"); jSplitPane2.setLeftComponent(originalPane); processedPane.setToolTipText("Processed Image"); jSplitPane2.setRightComponent(processedPane); jSplitPane1.setBottomComponent(jSplitPane2); pathLabel.setText("path"); browse.setText("browse..."); browse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseActionPerformed(evt); } }); path.setText("jTextField1"); path.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pathActionPerformed(evt); } }); customizeCameraButton.setText("customize Camera specifications"); customizeCameraButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { customizeCameraButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout .setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(27, 27, 27).addComponent(pathLabel) .addGap(18, 18, 18) .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(browse, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(97, 97, 97).addComponent(customizeCameraButton) .addContainerGap(676, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addContainerGap(52, Short.MAX_VALUE) .addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pathLabel, javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(browse, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(customizeCameraButton))) .addGap(33, 33, 33))); operationstab.addTab("image", jPanel2); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "default", "enhance contrast", "enhance brightness", "enhance sharpness", "blur" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jComboBox2.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "default", "enhance contrast", "enhance brightness", "enhance sharpness", "blur" })); jComboBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox2ActionPerformed(evt); } }); jComboBox3.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "default", "enhance contrast", "enhance brightness", "enhance sharpness", "blur" })); jComboBox3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox3ActionPerformed(evt); } }); jComboBox4.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "default", "enhance contrast", "enhance brightness", "enhance sharpness", "blur" })); jComboBox4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox4ActionPerformed(evt); } }); jComboBox5.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "default", "enhance contrast", "enhance brightness", "enhance sharpness", "blur" })); jComboBox5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox5ActionPerformed(evt); } }); process.setText("process"); process.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { processActionPerformed(evt); } }); jLabel1.setText("zoom level"); zoomSlider.setMajorTickSpacing(10); zoomSlider.setMaximum(20); zoomSlider.setMinimum(1); zoomSlider.setMinorTickSpacing(1); zoomSlider.setPaintTicks(true); zoomSlider.setValue(10); zoomSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { zoomSliderStateChanged(evt); } }); sharpSlider.setMajorTickSpacing(5); sharpSlider.setMaximum(20); sharpSlider.setMinimum(10); sharpSlider.setMinorTickSpacing(1); sharpSlider.setPaintTicks(true); sharpSlider.setValue(10); sharpSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { sharpSliderStateChanged(evt); } }); jLabel2.setText("sharpness"); reset.setText("reset"); reset.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { resetActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(jPanel3Layout.createSequentialGroup().addGap(74, 74, 74) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(zoomSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2))) .addGap(18, 18, 18) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(41, 41, 41).addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(sharpSlider, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(52, 52, 52) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(process).addComponent(reset, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(405, Short.MAX_VALUE))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(process)) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(reset, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout .createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(sharpSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(zoomSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))); operationstab.addTab("operations", jPanel3); LaplaceButton.setText("Laplace Transform"); LaplaceButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { LaplaceButtonActionPerformed(evt); } }); SobelButton.setText("Sobel Transform"); SobelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SobelButtonActionPerformed(evt); } }); ApplyTransformButton.setText("Apply Transform"); ApplyTransformButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ApplyTransformButtonActionPerformed(evt); } }); PrewittButton.setText("Prewitt Transform"); PrewittButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PrewittButtonActionPerformed(evt); } }); KirschButton.setText("Kirsch Transform"); KirschButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { KirschButtonActionPerformed(evt); } }); RobinsonButton.setText("Robinson Transform"); RobinsonButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RobinsonButtonActionPerformed(evt); } }); AnalyzeButton.setText("Analyze"); AnalyzeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AnalyzeButtonActionPerformed(evt); } }); cannyButton.setText("Apply canny operator "); cannyButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cannyButtonActionPerformed(evt); } }); jLabel4.setText("Select Transform : "); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap().addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel4Layout.createSequentialGroup().addComponent(ApplyTransformButton) .addGap(18, 18, 18).addComponent(cannyButton)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup().addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(LaplaceButton))) .addGap(18, 18, 18) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addComponent(SobelButton) .addGap(18, 18, 18).addComponent(PrewittButton).addGap(18, 18, 18) .addComponent(KirschButton).addGap(18, 18, 18).addComponent(RobinsonButton)) .addComponent(AnalyzeButton)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(20, 20, 20) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(LaplaceButton).addComponent(PrewittButton).addComponent(KirschButton) .addComponent(SobelButton).addComponent(RobinsonButton).addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ApplyTransformButton).addComponent(AnalyzeButton) .addComponent(cannyButton)) .addContainerGap())); operationstab.addTab("spatial filters", jPanel4); segmentButton.setText("segment"); segmentButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { segmentButtonActionPerformed(evt); } }); ratioButton.setText("ratio"); ratioButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ratioButtonActionPerformed(evt); } }); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout .setHorizontalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap() .addComponent(segmentButton).addGap(18, 18, 18) .addComponent(ratioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(1340, Short.MAX_VALUE))); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup().addContainerGap(58, Short.MAX_VALUE) .addGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(segmentButton).addComponent(ratioButton)) .addContainerGap())); operationstab.addTab("segmentation analysis", jPanel5); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(operationstab, javax.swing.GroupLayout.Alignment.TRAILING)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(operationstab).addContainerGap())); jSplitPane1.setLeftComponent(jPanel1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed str[0] = jComboBox1.getSelectedItem().toString(); //listModel.addElement(jComboBox1.getSelectedItem().toString()); // TODO add your handling code here: }//GEN-LAST:event_jComboBox1ActionPerformed private void browseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseActionPerformed System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // System.load("/usr/local/share/OpenCV/java/libopencv_java249.so"); fileChooser.setCurrentDirectory(new File("E:\\")); int returnVal = fileChooser.showOpenDialog(this); int i = 0; if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); FileFilter filter = new FileNameExtensionFilter("JPEG File", new String[] { "jpg", "jpeg", "tif", "gif" }); try { // IplImage img = cvLoadImage(file.getAbsolutePath()); ImageIcon ii = new ImageIcon(file.getAbsolutePath()); path.setText(file.getAbsolutePath()); originalPane.setViewportView(new JLabel(ii)); //image = ImageClass.getInstance(); ImageClass.getInstance().setImage(Highgui.imread(path.getText())); rgb2Gray(); listModel = new DefaultListModel(); } catch (Exception ex) { System.out.println("problem accessing file" + file.getAbsolutePath()); } } else { System.out.println("File access cancelled by user."); // TODO add your handling code here: } //GEN-LAST:event_browseActionPerformed } private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed str[1] = jComboBox2.getSelectedItem().toString(); // listModel.addElement(jComboBox2.getSelectedItem().toString()); // TODO add your handling code here: }//GEN-LAST:event_jComboBox2ActionPerformed private void pathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pathActionPerformed String str = path.getText(); ImageIcon ii = new ImageIcon(str); originalPane.setViewportView(new JLabel(ii)); ImageClass.getInstance().setImage(Highgui.imread(str)); // TODO add your handling code here: }//GEN-LAST:event_pathActionPerformed int i = 0; private void processActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processActionPerformed //rgb2Gray(); try { for (int k = 0; k < 7; k++) listModel.addElement(str[k]); } catch (NullPointerException e) { System.err.println("..........Please load a valid image.........."); } JList list = new JList(listModel); int i = 0; for (int j = listModel.getSize(); j > 0; j--, i++) { System.out.println(list.getModel().getElementAt(i).toString()); switch (list.getModel().getElementAt(i).toString()) { case "enhance contrast": { enhanceContrast(); break; } case "enhance brightness": { enhanceBrightness(0.9); break; } case "enhance sharpness": { enhanceSharpness(1.5); break; } case "blur": { blurImage(); break; } case "default": break; } } listModel.removeAllElements(); displayImage(); // TODO add your handling code here: }//GEN-LAST:event_processActionPerformed private void zoomSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_zoomSliderStateChanged int zoomfactor = zoomSlider.getValue(); zoomImage((double) zoomfactor / 10); try { displayImage(); } catch (OutOfMemoryError e) { System.err.println("Not Enough Space in the memory!!! Please try again !!!"); } // TODO add your handling code here: }//GEN-LAST:event_zoomSliderStateChanged private void sharpSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sharpSliderStateChanged int sharpfactor = sharpSlider.getValue(); enhanceSharpness((double) sharpfactor / 10); displayImage();// TODO add your handling code here: }//GEN-LAST:event_sharpSliderStateChanged private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed ImageClass.getInstance().setImage(Highgui.imread(path.getText())); rgb2Gray();// TODO add your handling code here: }//GEN-LAST:event_resetActionPerformed private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox3ActionPerformed str[2] = jComboBox3.getSelectedItem().toString(); //listModel.addElement(jComboBox3.getSelectedItem().toString()); // TODO add your handling code here: }//GEN-LAST:event_jComboBox3ActionPerformed private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox4ActionPerformed str[3] = jComboBox4.getSelectedItem().toString(); //listModel.addElement(jComboBox4.getSelectedItem().toString()); // TODO add your handling code here: }//GEN-LAST:event_jComboBox4ActionPerformed private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox5ActionPerformed str[4] = jComboBox5.getSelectedItem().toString(); //listModel.addElement(jComboBox5.getSelectedItem().toString()); // TODO add your handling code here: }//GEN-LAST:event_jComboBox5ActionPerformed private void ApplyTransformButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ApplyTransformButtonActionPerformed //System.out.println(radio); try { transform(hm.get(radio)); displayImage(); } catch (NullPointerException e) { System.err.println("..........Please load a valid Image.........."); } // TODO add your handling code here: }//GEN-LAST:event_ApplyTransformButtonActionPerformed private void LaplaceButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LaplaceButtonActionPerformed radio = evt.getActionCommand(); // TODO add your handling code here: }//GEN-LAST:event_LaplaceButtonActionPerformed private void SobelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SobelButtonActionPerformed radio = evt.getActionCommand(); // TODO add your handling code here: }//GEN-LAST:event_SobelButtonActionPerformed private void PrewittButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PrewittButtonActionPerformed radio = evt.getActionCommand(); // TODO add your handling code here: }//GEN-LAST:event_PrewittButtonActionPerformed private void KirschButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KirschButtonActionPerformed radio = evt.getActionCommand(); // TODO add your handling code here: }//GEN-LAST:event_KirschButtonActionPerformed private void RobinsonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RobinsonButtonActionPerformed radio = evt.getActionCommand(); // TODO add your handling code here: }//GEN-LAST:event_RobinsonButtonActionPerformed private void cannyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cannyButtonActionPerformed try { Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); threshold(source, destination, 50, 255, CV_THRESH_BINARY); // ImageClass.getInstance().setImage(destination); // Imgproc.GaussianBlur(destination,destination,new org.opencv.core.Size(0,0),10); // fastNlMeansDenoising(destination,destination,3,7,21); // source = ImageClass.getInstance().getImage(); Imgproc.Canny(destination, destination, 0.05, 0.15, 3, true); ImageClass.getInstance().setImage(destination); } catch (NullPointerException e) { System.err.println("..........Please load a valid Image.........."); } displayImage(); // TODO add your handling code here: }//GEN-LAST:event_cannyButtonActionPerformed private void AnalyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnalyzeButtonActionPerformed try { double pixelArea = 1; double imageSize = 1; if (magnification == 50) pixelArea = 1.2996; else if (magnification == 100) pixelArea = 0.329476; else if (magnification == 200) pixelArea = 0.08162; else { imageSize = pow(10, 10) * pow(magnification, -2); pixelArea = (imageSize) / (resolution1 * resolution2); } Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); //Imgproc.adaptiveThreshold(source,destination,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY_INV,13,2); //Imgproc.GaussianBlur(destination,destination,new org.opencv.core.Size(0,0),5); threshold(source, destination, 30, 255, CV_THRESH_BINARY); distanceTransform(destination, destination, CV_DIST_L2, 3); normalize(destination, destination, 0, 1, NORM_MINMAX); threshold(destination, destination, 0.5, 1, CV_THRESH_BINARY); destination.convertTo(destination, CV_8U); /*ImageClass.getInstance().setImage(destination); displayImage();*/ ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); MatOfInt4 hierarchy = new MatOfInt4(); // Rect roi = new Rect(100,100,destination.cols()-100,destination.rows()-100); // Mat imageROI = destination.submat(roi); /*ImageClass.getInstance().setImage(imageROI); displayImage();*/ //Imgproc.Canny(source,destination,0.05,0.15); Imgproc.findContours(destination, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); int total = contours.size(); int count = 0; //System.out.println(total); MatOfPoint[] cntrs = contours.toArray(new MatOfPoint[contours.size()]); ArrayList<Double> value = new ArrayList<Double>(); double temp = 0; for (int i = 0; i < contours.size(); i++) { if (contourArea(cntrs[i]) > 1/*&& contourArea(cntrs[i])<3000*/) { temp = 2 * Math.sqrt((contourArea(cntrs[i]) * (pixelArea)) / Math.PI); //temp = contourArea(cntrs[i]); if (temp > 0) { value.add(count, temp); System.out.println("area of contour " + count++ + " is : " + contourArea(cntrs[i])); } } } System.out.println("total number of contours : " + count); double[] values = new double[count]; for (int i = 0; i < count; i++) { //temp = value.get(i);//2*Math.sqrt((value.get(i)*127.024)/Math.PI); //if(temp>0) values[i] = value.get(i); //System.out.println("the diameter of particle "+i+ "is "+values[i]); // values[i]=(contourArea(cntrs[i])*127.024)/100; } //int number = 300; /*HistogramDataset dataset = new HistogramDataset(); dataset.setType(HistogramType.FREQUENCY); XYSeries series = new XYSeries("Particle Size Distribution"); try{ dataset.addSeries("Histogram1",values,number,0,300); for(int i=0;i<300;i++){ if(dataset.getYValue(0,i)>0) series.add(dataset.getXValue(0,i),dataset.getYValue(0,i)); } // XYDataset xydataset = new XYSeriesCollection(series); }catch(Exception e) { e.printStackTrace(); } XYDataset xydataset = new XYSeriesCollection(series); String plotTitle = "Particle Size Distribution"; String xAxis = "particle diameter in microns"; String yAxis = "particle count"; PlotOrientation orientation = PlotOrientation.VERTICAL; boolean show = true; boolean toolTips = true; boolean urls = false; JFreeChart chart1 = ChartFactory.createXYLineChart(plotTitle,xAxis,yAxis,xydataset,orientation,show,toolTips,urls); JFreeChart chart2 = ChartFactory.createHistogram(plotTitle,xAxis,yAxis,dataset,orientation,show,toolTips,urls); int width1 = 500; int height1 = 500; ChartFrame frame1 = new ChartFrame("Coal PSD",chart1); frame1.setSize(width1,height1); frame1.setVisible(true); frame1.setDefaultCloseOperation(DISPOSE_ON_CLOSE); int width2 = 500; int height2 = 500; ChartFrame frame2 = new ChartFrame("Coal PSD",chart2); frame2.setSize(width2,height2); frame2.setVisible(true); frame2.setDefaultCloseOperation(DISPOSE_ON_CLOSE);*/ } catch (NullPointerException e) { System.err.println("..........Please load a valid Image.........."); } // TODO add your handling code here: }//GEN-LAST:event_AnalyzeButtonActionPerformed private void segmentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_segmentButtonActionPerformed // create new KMeans object Mat source = ImageClass.getInstance().getImage(); KMeans kmeans = new KMeans(); BufferedImage src = kmeans.matToBuff(source); // call the function to actually start the clustering BufferedImage dst = kmeans.calculate(src, 2, KMeans.MODE_ITERATIVE); WaterShed ws = new WaterShed(); /* ws.floodPoints = 22; ws.windowWidth = 60; ws.connectedPixels = 8; BufferedImage src = ws.matToBuff(source); BufferedImage dst = ws.calculate(src,100,60,8);*/ //ImageClass.getInstance().setImage(); /* ImageIcon ii = new ImageIcon(dst); processedPane.setViewportView(new JLabel(ii));*/ Mat finalImage = ws.buffToMat(dst); ImageClass.getInstance().setImage(finalImage); displayImage(); // TODO add your handling code here: }//GEN-LAST:event_segmentButtonActionPerformed private void customizeCameraButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_customizeCameraButtonActionPerformed new cameraFrame("customize camera specifications"); // TODO add your handling code here: }//GEN-LAST:event_customizeCameraButtonActionPerformed private void ratioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ratioButtonActionPerformed Mat source = ImageClass.getInstance().getImage(); KMeans kmeans = new KMeans(); BufferedImage src = kmeans.matToBuff(source); double[] bins = new double[256]; for (int i = 0; i < 256; i++) bins[i] = -1; int number = 500; for (int i = 0; i < src.getWidth(); i++) { for (int j = 0; j < src.getHeight(); j++) { // int p = src.getData().getSample(i,j,0); int p = src.getRGB(i, j) & 0xFF; //System.out.println(p); (bins[p])++; //System.out.println(bins[p]++); } } /*HistogramDataset dataset = new HistogramDataset(); dataset.setType(HistogramType.FREQUENCY); try{ dataset.addSeries("Histogram2",bins,number,0,300); }catch(Exception e){ e.printStackTrace(); } String plotTitle = "Coal & Ash Analysis"; String xAxis = "pixel intensity"; String yAxis = "pixel count"; PlotOrientation orientation = PlotOrientation.VERTICAL; boolean show = true; boolean toolTips = true; boolean urls = false; JFreeChart chart = ChartFactory.createHistogram(plotTitle,xAxis,yAxis,dataset,orientation,show,toolTips,urls); int width = 800; int height = 600; ChartFrame frame1 = new ChartFrame("Coal & ash Analysis",chart); frame1.setSize(width,height); frame1.setVisible(true); frame1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);*/ // TODO add your handling code here: }//GEN-LAST:event_ratioButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ImageGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ImageGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ImageGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ImageGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ImageGUI().setVisible(true); } }); } private void rgb2Gray() { //String str = path.getText().toString(); Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), CV_8UC1); Imgproc.cvtColor(source, destination, COLOR_RGB2GRAY); ImageClass.getInstance().setImage(destination); } private void zoomImage(double zoomlevel) { Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat((int) (source.rows() * zoomlevel), (int) (source.cols() * zoomlevel), source.type()); Imgproc.resize(source, destination, destination.size(), zoomlevel, zoomlevel, INTER_CUBIC); ImageClass.getInstance().setImage(destination); zoomSlider.setValue((int) (zoomlevel * 10)); } private void enhanceContrast() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //System.load("/usr/local/share/OpenCV/java/libopencv_java249.so"); // rgb2Gray(); Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Imgproc.equalizeHist(source, destination); image.getInstance().setImage(destination); } private void enhanceSharpness(double alpha) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // System.load("/usr/local/share/OpenCV/java/libopencv_java249.so"); Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); /// GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX); Imgproc.GaussianBlur(source, destination, new org.opencv.core.Size(0, 0), 10); ///addWeighted(Mat src1, double alpha, Mat src2, double beta, double gamma, Mat dst); Core.addWeighted(source, alpha, destination, -0.5, 0, destination); ImageClass.getInstance().setImage(destination); sharpSlider.setValue((int) (alpha * 10)); } private void enhanceBrightness(double alpha) { Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); source.convertTo(destination, -1, alpha, 50); ImageClass.getInstance().setImage(destination); // brightSlider.setValue((int)(alpha*10)); } private void blurImage() { Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.size(), source.type()); Imgproc.GaussianBlur(source, destination, new org.opencv.core.Size(0, 0), 2); // Imgproc.bilateralFilter(source, destination, , sensorSize, sensorSize, WIDTH); ImageClass.getInstance().setImage(destination); } private void transform(final int[][] arr) { try { int kernelSize = 3; System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //System.load("/usr/local/share/OpenCV/java/libopencv_java249.so"); // System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Mat kernel = new Mat(kernelSize, kernelSize, CvType.CV_32F) { { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.println(arr[i][j]); put(i, j, arr[i][j]); } } } }; Imgproc.filter2D(source, destination, -1, kernel); ImageClass.getInstance().setImage(destination); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } private void displayImage() { Mat printimg = ImageClass.getInstance().getImage(); MatOfByte mob = new MatOfByte(); Highgui.imencode(".jpg", printimg, mob); byte[] byteArray = mob.toArray(); BufferedImage bufim = null; try { InputStream in = new ByteArrayInputStream(byteArray); bufim = ImageIO.read(in); } catch (Exception e) { e.printStackTrace(); } ImageIcon ii = new ImageIcon(bufim); processedPane.setViewportView(new JLabel(ii)); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton AnalyzeButton; private javax.swing.JButton ApplyTransformButton; private javax.swing.JRadioButton KirschButton; private javax.swing.JRadioButton LaplaceButton; private javax.swing.JRadioButton PrewittButton; private javax.swing.JRadioButton RobinsonButton; private javax.swing.JRadioButton SobelButton; private javax.swing.JButton browse; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.JButton cannyButton; private javax.swing.JButton customizeCameraButton; private javax.swing.JFileChooser fileChooser; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox2; private javax.swing.JComboBox jComboBox3; private javax.swing.JComboBox jComboBox4; private javax.swing.JComboBox jComboBox5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JSplitPane jSplitPane2; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTabbedPane operationstab; private javax.swing.JScrollPane originalPane; private javax.swing.JTextField path; private javax.swing.JLabel pathLabel; private javax.swing.JButton process; private javax.swing.JScrollPane processedPane; private javax.swing.JButton ratioButton; private javax.swing.JButton reset; private javax.swing.JButton segmentButton; private javax.swing.JSlider sharpSlider; private javax.swing.JSlider zoomSlider; // End of variables declaration//GEN-END:variables }