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 opencv.fark; import com.sun.org.apache.bcel.internal.generic.AALOAD; import java.awt.AlphaComposite; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFileChooser; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.StyleConstants; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; /** * * @author sony */ public class ResimSecMainFrame extends javax.swing.JFrame { /** * Creates new form ResimSecMainFrame */ private static Mat resim1; private static Mat resim2; private Mat element; Graphics g; Graphics g1; Graphics g2; Thread t1; Thread t2; private static int farkli_pixel_say; public ResimSecMainFrame() { initComponents(); g = jPanel1.getGraphics(); g1 = jPanel2.getGraphics(); farkli_pixel_say = 0; element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)); } /** * 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() { jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButtonFarkBul = new javax.swing.JButton(); jProgressBar1 = new javax.swing.JProgressBar(); jButtonBGRKarsilastir = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); jLabelPixelCount = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabelYuzdeHata = new javax.swing.JLabel(); jButtonKarsilastirV1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 638, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 478, Short.MAX_VALUE)); jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 638, Short.MAX_VALUE)); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE)); jButton1.setText("RESM SE"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("RESM SE"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Karsilastir"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButtonFarkBul.setText("Fark"); jButtonFarkBul.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonFarkBulActionPerformed(evt); } }); jButtonBGRKarsilastir.setText("BGR Karlatr"); jButtonBGRKarsilastir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonBGRKarsilastirActionPerformed(evt); } }); jLabel1.setText("Farkl Pixel Says : "); jLabel2.setText("Hata % :"); jButtonKarsilastirV1.setText("Karlatr v1"); jButtonKarsilastirV1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonKarsilastirV1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jButton2).addGap(0, 797, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButtonBGRKarsilastir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonFarkBul, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1).addGap(18, 18, 18) .addComponent(jLabelPixelCount)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2).addGap(18, 18, 18) .addComponent(jLabelYuzdeHata))) .addGap(0, 0, Short.MAX_VALUE)))))) .addGroup(layout.createSequentialGroup().addComponent(jButtonKarsilastirV1).addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(32, 32, 32) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButtonFarkBul, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButtonBGRKarsilastir, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(39, 39, 39) .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabelPixelCount)) .addGap(33, 33, 33) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jLabelYuzdeHata))) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18).addComponent(jButtonKarsilastirV1) .addContainerGap(52, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF & BMP Images", "png"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName() + chooser.getSelectedFile().getPath()); String url = null; try { url = chooser.getSelectedFile().getPath(); System.out.println(url); resim1 = Highgui.imread(url.toString()); // BufferedImage b = ImageIO.read(url); // BufferedImage b1 = new BufferedImage(b.getWidth(), b.getHeight(), BufferedImage.TYPE_3BYTE_BGR); // b1=b; // byte [] pixels = ((DataBufferByte)b1.getRaster().getDataBuffer()).getData(); // // resim1 = new Mat(b1.getHeight(),b1.getWidth(),CvType.CV_8UC3); // resim1.put(0, 0, pixels); // System.out.println(resim1.cols()); // g.drawImage(b, 0, 0,b.getWidth(),b.getHeight(), null); MatToBufImg c = new MatToBufImg(); c.setMatrix(resim1, ".png"); g.drawImage(c.getBufferedImage(), 0, 0, null); } catch (Exception e) { } } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF & BMP Images", "png"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName() + chooser.getSelectedFile().getPath()); String url = null; try { url = chooser.getSelectedFile().getPath(); resim2 = Highgui.imread(url); // BufferedImage b = ImageIO.read(url); // BufferedImage b1 = new BufferedImage(b.getWidth(), b.getHeight(), BufferedImage.TYPE_3BYTE_BGR); // b1=b; // byte [] pixels = ((DataBufferByte)b1.getRaster().getDataBuffer()).getData(); // // resim2 = new Mat(b1.getHeight(),b1.getWidth(),CvType.CV_8UC3); // resim2.put(0, 0, pixels); // System.out.println(resim2.cols()); //// g.drawImage(b, 0, 0,b.getWidth(),b.getHeight(), null); // g1.drawImage(b, 0, 0,b.getWidth(),b.getHeight(), null); // MatToBufImg c = new MatToBufImg(); c.setMatrix(resim2, ".png"); g1.drawImage(c.getBufferedImage(), 0, 0, null); } catch (Exception E) { } } }//GEN-LAST:event_jButton2ActionPerformed // iki resim arasnda fark bulma Yntem 1 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed Mat sonuc = new Mat(resim1.rows(), resim1.cols(), CvType.CV_8UC1); Mat gray1 = new Mat(resim1.rows(), resim1.cols(), CvType.CV_8UC1); Mat gray2 = new Mat(resim1.rows(), resim1.cols(), CvType.CV_8UC1); Imgproc.cvtColor(resim1, gray1, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(resim2, gray2, Imgproc.COLOR_BGR2GRAY); MatToBufImg matToBufImage = new MatToBufImg(); // matToBufImage.setMatrix(gray1, ".jpg"); // g.drawImage(matToBufImage.getBufferedImage(), 0, 0, null); // matToBufImage.setMatrix(gray2, ".jpg"); // g1.drawImage(matToBufImage.getBufferedImage(), 0, 0, null); Core.absdiff(gray1, gray2, sonuc); Imgproc.blur(sonuc, sonuc, new Size(10, 10)); Imgproc.threshold(sonuc, sonuc, 10, 255, Imgproc.THRESH_BINARY); Imgproc.erode(sonuc, sonuc, element); Imgproc.dilate(sonuc, sonuc, element); farkli_pixel_say = 0; float boyut = sonuc.cols() * sonuc.rows(); for (int i = 0; i < sonuc.cols(); i++) { for (int j = 0; j < sonuc.rows(); j++) { double a[] = sonuc.get(j, i); if (a[0] == 255) { farkli_pixel_say++; jLabelPixelCount.setText(String.valueOf(farkli_pixel_say)); } } } float hata = (farkli_pixel_say / boyut) * 100; if (hata == 0) { jLabelYuzdeHata.setText("Hata Yok !"); } else { jLabelYuzdeHata.setText(String.valueOf(hata).substring(0, 5)); } Imgproc.cvtColor(sonuc, sonuc, Imgproc.COLOR_GRAY2BGR); Core.bitwise_or(resim2, sonuc, sonuc); matToBufImage.setMatrix(sonuc, ".png"); g1.drawImage(matToBufImage.getBufferedImage(), 0, 0, null); }//GEN-LAST:event_jButton3ActionPerformed //Yntem 2 private void jButtonFarkBulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFarkBulActionPerformed farkli_pixel_say = 0; Thread t = new Thread() { @Override public void run() { Mat sonuc = new Mat(resim1.rows(), resim1.cols(), CvType.CV_8UC1); Mat gray1 = new Mat(resim1.rows(), resim1.cols(), CvType.CV_8UC1); Mat gray2 = new Mat(resim1.rows(), resim1.cols(), CvType.CV_8UC1); System.out.println(gray1.channels()); Imgproc.cvtColor(resim1, gray1, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(resim2, gray2, Imgproc.COLOR_BGR2GRAY); for (int i = 0; i < gray1.rows(); i++) { for (int j = 0; j < gray1.cols(); j++) { double[] kresim1 = gray1.get(i, j); double[] kresim2 = gray2.get(i, j); //System.out.println(resim1[0]+" "+resim2[0]); if (kresim1[0] != kresim2[0]) { // System.out.println(kresim1[0]); g1.setFont(new Font("TimesRoman", Font.PLAIN, 2)); g1.drawString("*", j, i); //gray2.put(i, j, kresim2[0]*255); jLabelPixelCount.setText(String.valueOf(++farkli_pixel_say)); } } jProgressBar1.setValue(i); } //forend // Mat a = new Mat(); // Core.compare(resim1, resim2, a, Core.CMP_NE); // Imgproc.blur(gray2, gray2, new Size(10, 10)); // MatToBufImg matToBufImage = new MatToBufImg(); // matToBufImage.setMatrix(gray2, ".jpg"); // g1.drawImage(matToBufImage.getBufferedImage(), 0, 0, null); } }; t.start(); }//GEN-LAST:event_jButtonFarkBulActionPerformed //Yntem 3 private void jButtonBGRKarsilastirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBGRKarsilastirActionPerformed Thread t = new Thread() { @Override public void run() { // Mat C = resim1.clone(); for (int i = 0; i < resim1.rows(); i++) { for (int j = 0; j < resim1.cols(); j++) { double[] data = resim1.get(i, j); double[] data1 = resim2.get(i, j); if (data[0] != data1[0] || data[1] != data1[1] || data[2] != data1[2]) { g1.drawString("o", j, i); /* data1[1] = 0; data1[2] = 0; data1[0] = 0; resim2.put(i, j, data1); */ } } jProgressBar1.setValue(i); } // MatToBufImg matToBufferedImage = new MatToBufImg(); // matToBufferedImage.setMatrix(resim2, ".jpg"); // g1.drawImage(matToBufferedImage.getBufferedImage(), 0, 0, null); } }; t.start(); }//GEN-LAST:event_jButtonBGRKarsilastirActionPerformed // Yntem 4 private void jButtonKarsilastirV1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonKarsilastirV1ActionPerformed Mat resim1hsv = new Mat(); Mat resim2hsv = new Mat(); Imgproc.cvtColor(resim1, resim1hsv, Imgproc.COLOR_BGR2HSV); Imgproc.cvtColor(resim2, resim2hsv, Imgproc.COLOR_BGR2HSV); Mat image = resim1.clone(); double data2[] = new double[3]; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { double[] data = resim1.get(i, j); double[] data1 = resim2.get(i, j); data2[0] = Math.abs(data[0] - data1[0]); data2[1] = Math.abs(data[1] - data1[1]); data2[2] = Math.abs(data[2] - data1[2]); image.put(i, j, data2); } } Core.inRange(image, new Scalar(1, 0, 0), new Scalar(255, 255, 255), image); //Imgproc.threshold(image, image, 0, 256, Imgproc.THRESH_BINARY); MatToBufImg buf = new MatToBufImg(); buf.setMatrix(image, ".png"); g.drawImage(buf.getBufferedImage(), 0, 0, null); }//GEN-LAST:event_jButtonKarsilastirV1ActionPerformed /** * @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(ResimSecMainFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ResimSecMainFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ResimSecMainFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ResimSecMainFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> System.loadLibrary(Core.NATIVE_LIBRARY_NAME); /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ResimSecMainFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButtonBGRKarsilastir; private javax.swing.JButton jButtonFarkBul; private javax.swing.JButton jButtonKarsilastirV1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabelPixelCount; private javax.swing.JLabel jLabelYuzdeHata; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JProgressBar jProgressBar1; // End of variables declaration//GEN-END:variables }