opencv.fark.VideoSecMainFrame.java Source code

Java tutorial

Introduction

Here is the source code for opencv.fark.VideoSecMainFrame.java

Source

/*
 * 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 java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.opencv.core.Core;
import org.opencv.core.CvException;
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.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;

/**
 *
 * @author sony
 */
public class VideoSecMainFrame extends javax.swing.JFrame implements MouseListener, MouseMotionListener {

    /**
     * Creates new form VideoSecMainFrame
     */
    private final Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));

    private Mat kesit;
    private Mat frame;
    private Mat reference_section;
    private Point staticFirstPoint;
    private Point staticSecondPoint;
    private Graphics g;
    private Graphics g1;
    private Point first_point;
    private Point second_point;
    private boolean drag;
    private int referans_degeri;
    private VideoCapture videoCapture;
    private Thread t;
    private Thread t1;
    private static int say;

    public VideoSecMainFrame() {
        initComponents();

        g = jPanel1.getGraphics();
        g1 = jPanel2.getGraphics();
        referans_degeri = 0;
        drag = false;
        reference_section = new Mat();
        staticFirstPoint = new Point(0, 0);
        staticSecondPoint = new Point(0, 0);
        say = 0;

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (videoCapture != null && videoCapture.isOpened()) {
                    t.stop();
                    videoCapture.release();
                    t.interrupt();
                }
                System.exit(0);
            }

        });

        jPanel1.addMouseListener(this);
        jPanel1.addMouseMotionListener(this);
    }

    /**
     * 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();
        jButtonRefAL = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButtonKarsilastir = new javax.swing.JButton();
        list1 = new java.awt.List();
        label1 = new java.awt.Label();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemNewVideo = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 640, Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 0, Short.MAX_VALUE));

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setPreferredSize(new java.awt.Dimension(640, 480));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 636, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 476, Short.MAX_VALUE));

        jButton1.setText("Video Se");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButtonRefAL.setText("Referans AL");
        jButtonRefAL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRefALActionPerformed(evt);
            }
        });

        jButtonKarsilastir.setText("Karlatr");
        jButtonKarsilastir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonKarsilastirActionPerformed(evt);
            }
        });

        label1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        label1.setText("Bozukluk Oran :");

        jButton2.setText("Balat");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItemNewVideo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N,
                java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemNewVideo.setText("New Video");
        jMenuItemNewVideo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewVideoActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemNewVideo);

        jMenuItem1.setText("Resim Karsilastir");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        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()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jPanel2, 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(list1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 0, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE)
                                                        .addComponent(
                                                                label1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addGroup(layout.createSequentialGroup().addComponent(jButtonRefAL)
                                                .addGap(18, 18, 18).addComponent(jButtonKarsilastir)
                                                .addGap(18, 18, 18).addComponent(jButton2))
                                        .addComponent(jButton1))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 42,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                        layout.createSequentialGroup()
                                                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(list1, javax.swing.GroupLayout.PREFERRED_SIZE, 200,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButtonRefAL).addComponent(jButtonKarsilastir).addComponent(jButton2))
                        .addGap(18, 18, 18).addComponent(jLabel1).addContainerGap(34, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    boolean videoDeger;
    String url = null;

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        referans_degeri = 0;
        drag = false;

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF & BMP Images", "jpg", "gif", "bmp",
                "mp4", "avi");

        chooser.setFileFilter(filter);

        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            //        if (!(chooser.getSelectedFile().getName().substring(chooser.getSelectedFile().getName().lastIndexOf('.')+1)).equals("mp4") ) {
            //            System.out.println("Ltfen mp4 sein");
            //            return;
            //        }
            System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()
                    + chooser.getSelectedFile().getPath());

            url = chooser.getSelectedFile().getPath();

            t = new Thread(new VideoOynat());

            t.start();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    class VideoOynat implements Runnable {
        MatToBufImg matToBufferedImageConverter = new MatToBufImg();

        @Override
        public void run() {

            try {

                //               Desktop.getDesktop().open(new File(url));

                videoCapture = new VideoCapture(url);
                //          videoCapture = new VideoCapture(0);
                //  Thread.sleep(3000);
                if (!videoCapture.isOpened()) {
                    System.out.println("Video Alamad");
                }

                double fps = videoCapture.get(5);

                System.out.println("FPS :" + fps);

                frame = new Mat(480, 640, CvType.CV_8UC3);

                Mat hsv_image = new Mat();
                Mat thresholded = new Mat();

                while (true) {
                    boolean basarili = videoCapture.read(frame);

                    if (!basarili) {

                        System.out.println("Okunamyor");
                        t1.stop();
                        t.stop();
                        t1.interrupt();
                        t.interrupt();
                        //                break;
                        //   videoCapture = new  VideoCapture(url);
                        // videoCapture.read(frame); //video bitti tekrar baa
                    }

                    if (drag && referans_degeri == 0 && first_point != null && second_point != null) {
                        Core.rectangle(frame, first_point, second_point, new Scalar(0, 0, 255));
                        //  System.out.println("REF0: "+first_point+" "+second_point);
                    }
                    if (referans_degeri == 1) {
                        Core.rectangle(frame, first_point, second_point, new Scalar(255, 0, 0));
                        //  System.out.println("REF1: "+first_point+" "+second_point);
                    }

                    /*
                    Imgproc.cvtColor(frame, hsv_image, Imgproc.COLOR_BGR2HSV);
                    Core.inRange(hsv_image, new Scalar(170, 150, 60), new Scalar(179 , 255, 255), thresholded);
                    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
                    Imgproc.findContours(thresholded, contours, new Mat(),Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0));
                    for(int i=0; i< contours.size();i++){
                    //  System.out.println(Imgproc.contourArea(contours.get(i)));
                    //  if (Imgproc.contourArea(contours.get(i)) > 1 ){
                            Rect rect = Imgproc.boundingRect(contours.get(i));
                            kesit = frame.submat(rect);
                            //  System.out.println(rect.height);
                       // if (rect.height > 20 && rect.height <30 && rect.width < 30 && rect.width >20){
                       //  System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width);
                       Core.rectangle(frame, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
                     // Core.circle(webcam_image, new Point(rect.x+rect.height/2, rect.y+rect.width/2), i, new Scalar(0, 0, 255));
                        //}
                     //}
                    }
                    */

                    matToBufferedImageConverter.setMatrix(frame, ".jpg");
                    g.drawImage(matToBufferedImageConverter.getBufferedImage(), 0, 0, frame.cols(), frame.rows(),
                            null);
                } // while end       
            } catch (Exception e) {
            }

            // void run end
        }

    }

    private void jButtonRefALActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRefALActionPerformed
        referans_degeri = 1;
        jLabel1.setForeground(Color.green);
        jLabel1.setOpaque(true);
        jLabel1.setBackground(Color.black);
        jLabel1.setFont(new Font("Serif", Font.PLAIN, 20));
        jLabel1.setText("Referans Alnd ! Karlatr butonuna basnz.");
        MatToBufImg matToBufImage = new MatToBufImg();
        matToBufImage.setMatrix(reference_section, ".png");
        JLabel picLabel = new JLabel(new ImageIcon(matToBufImage.getBufferedImage()));
        JFrame j = new JFrame("Referans Resim");
        JPanel panel = new JPanel();

        panel.add(picLabel);
        panel.setVisible(true);
        j.add(panel);
        j.setSize(reference_section.width() + 30, reference_section.width() + 30);
        j.setVisible(true);

        //  JOptionPane.showMessageDialog(null, picLabel, "About", JOptionPane.PLAIN_MESSAGE, null);

    }//GEN-LAST:event_jButtonRefALActionPerformed

    Mat temp = null;

    private void jButtonKarsilastirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonKarsilastirActionPerformed

        if (referans_degeri == 1) {
            t1 = new Thread() {

                @Override
                public void run() {
                    try {
                        while (!interrupted()) {
                            //System.out.println("Karlatr");
                            Rect r = new Rect(staticFirstPoint, staticSecondPoint);
                            temp = frame.submat(r);
                            jPanel2.repaint();
                            MatToBufImg matToBufImage = new MatToBufImg();
                            matToBufImage.setMatrix(temp, ".png");
                            g1.drawImage(matToBufImage.getBufferedImage(),
                                    jPanel2.getWidth() / 2 - reference_section.cols() / 2,
                                    jPanel2.getHeight() / 2 - reference_section.rows() / 2, null);
                            //Highgui.imwrite("D:\\resim1.jpg", temp);

                            //metod 3 sn yede bir fark hesapla
                            theDifferenceBetweenTheTwoImages(reference_section, temp);
                            Thread.sleep(3000);
                        }
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }

                private void theDifferenceBetweenTheTwoImages(Mat ref, Mat tem) {

                    Mat sonuc = new Mat(tem.rows(), tem.cols(), CvType.CV_8UC1);
                    Mat gray1 = new Mat(tem.rows(), tem.cols(), CvType.CV_8UC1);
                    Mat gray2 = new Mat(reference_section.rows(), reference_section.cols(), CvType.CV_8UC1);

                    Imgproc.cvtColor(tem, gray1, Imgproc.COLOR_BGR2GRAY);
                    Imgproc.cvtColor(ref, gray2, Imgproc.COLOR_BGR2GRAY);

                    MatToBufImg matToBufImage = new MatToBufImg();

                    //        matToBufImage.setMatrix(gray2, ".jpg");
                    //        g1.drawImage(matToBufImage.getBufferedImage(), 0, 0, null);
                    //        
                    //        matToBufImage.setMatrix(gray1, ".jpg");
                    //        g1.drawImage(matToBufImage.getBufferedImage(), 0, 0, null);

                    Core.absdiff(gray2, gray1, sonuc);
                    Imgproc.blur(sonuc, sonuc, new Size(15, 15));
                    Imgproc.threshold(sonuc, sonuc, 2, 255, Imgproc.THRESH_BINARY);

                    Imgproc.dilate(sonuc, sonuc, element);
                    Imgproc.erode(sonuc, sonuc, element);

                    //  List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
                    int sayac = 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) {
                                sayac++;

                            }
                        }
                    }

                    float hata = (sayac / boyut) * 100;
                    System.out.println("Bozukluk oran % :" + hata);
                    if (hata == 0) {
                        list1.add("Hatasiz!", 0);
                        Highgui.imwrite("D:\\testResim\\hatasiz\\ref" + (++say) + ".png", tem);
                    } else {
                        list1.add("%" + String.valueOf(hata).substring(0, 5), 0);

                        Highgui.imwrite("D:\\testResim\\hatali\\yuzde" + String.valueOf(hata).substring(0, 2)
                                + "bozuk" + (++say) + ".png", tem);
                    }
                    Imgproc.cvtColor(sonuc, sonuc, Imgproc.COLOR_GRAY2BGR);
                    Core.bitwise_and(sonuc, tem, sonuc);
                    matToBufImage.setMatrix(sonuc, ".png");
                    g1.drawImage(matToBufImage.getBufferedImage(),
                            jPanel2.getWidth() / 2 - reference_section.cols() / 2,
                            jPanel2.getHeight() / 2 - reference_section.rows() / 2, null);
                    //        Highgui.imwrite("D:\\farkResim.jpg",sonuc);
                }
            };
            t1.start();

        } else {
            jLabel1.setForeground(Color.red);
            jLabel1.setOpaque(true);
            jLabel1.setBackground(Color.black);
            jLabel1.setFont(new Font("Serif", Font.PLAIN, 20));
            jLabel1.setText("Referans Resim Seiniz!");
        }
    }//GEN-LAST:event_jButtonKarsilastirActionPerformed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        if (videoCapture != null && videoCapture.isOpened()) {
            t.stop();
            videoCapture.release();
            if (t1 != null) {
                t1.stop();
                t1.interrupt();
            }
            t.interrupt();
        }
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemNewVideoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewVideoActionPerformed
        jButton1ActionPerformed(evt);
    }//GEN-LAST:event_jMenuItemNewVideoActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        if (jButton2.getText().equals("Balat") && url != null) {
            if (!t.isAlive()) {
                referans_degeri = 0;
                drag = false;
                t = new Thread(new VideoOynat());
                t.start();
                jButton2.setText("Durdur");
            }

            videoDeger = true;
        } else if (jButton2.getText().equals("Durdur")) {

            if (t1 != null) {
                t1.stop();
                t1.interrupt();
            }
            t.stop();
            t.interrupt();

            jButton2.setText("Balat");
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        ResimSecMainFrame m = new ResimSecMainFrame();

        m.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    @Override
    public void mousePressed(MouseEvent me) {
        if (referans_degeri == 0) {
            System.out.println("burdaym");
            first_point = new Point(me.getX(), me.getY());
            staticFirstPoint.x = first_point.x;
            staticFirstPoint.y = first_point.y;
            System.out.println("staticfirst" + staticFirstPoint);
            // System.out.println("firt"+me.getX()+" "+me.getY());
            jPanel2.repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        if (referans_degeri == 0) {
            drag = true;
            second_point = new Point(me.getX(), me.getY());
            staticSecondPoint.x = second_point.x;
            staticSecondPoint.y = second_point.y;
            //        System.out.println(staticSecondPoint);
            //        System.out.println(me.getX()+" "+me.getY()); 
        }

    }

    @Override
    public void mouseReleased(MouseEvent me) {
        if (drag && referans_degeri == 0) {
            //            System.out.println("Mouse brakma eylemi aktif");
            //            System.out.println("firstpoint"+first_point);
            try {
                if (staticFirstPoint.x < staticSecondPoint.x) {
                    staticFirstPoint.x += 1;
                    staticFirstPoint.y += 1;
                } else {
                    staticSecondPoint.x += 1;
                    staticSecondPoint.y += 1;
                }

                Rect r1 = new Rect(staticFirstPoint, staticSecondPoint);
                Mat ref = new Mat();
                frame.copyTo(ref);
                reference_section = ref.submat(r1);

                //                MatToBufImg matToBufImage = new MatToBufImg();
                //                matToBufImage.setMatrix(reference_section, ".jpg");
                //                g1.drawImage(matToBufImage.getBufferedImage(), 0, 0, null);
                Highgui.imwrite("D:\\testResim\\refResim.png", reference_section);
            } catch (CvException e) {
                System.out.println(" OpenCV Sorun Olutu");
            } catch (Exception ex) {
                System.out.println("Java sorun Olutu");
            }
        } // first if end
        drag = false;
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }

    @Override
    public void mouseMoved(MouseEvent me) {
    }

    /**
     * @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(VideoSecMainFrame.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VideoSecMainFrame.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VideoSecMainFrame.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VideoSecMainFrame.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Dikkat */
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VideoSecMainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonKarsilastir;
    private javax.swing.JButton jButtonRefAL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemNewVideo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private java.awt.Label label1;
    private java.awt.List list1;
    // End of variables declaration//GEN-END:variables

}