Example usage for org.opencv.core Mat cols

List of usage examples for org.opencv.core Mat cols

Introduction

In this page you can find the example usage for org.opencv.core Mat cols.

Prototype

public int cols() 

Source Link

Usage

From source file:opencv.CaptchaDetection.java

private static int row_length(Mat src, int dst_row, int dst_col) {

    int length = 0;

    for (int col = dst_col; col >= 0; col--) {
        if (src.get(dst_row, col)[0] == 255)
            length++;// w  ww.  jav a  2s  .c  o  m
        else
            break;
    }

    for (int col = dst_col + 1; col < src.cols(); col++) {
        if (src.get(dst_row, col)[0] == 255)
            length++;
        else
            break;
    }

    //out.println(length);

    return length;
}

From source file:opencv.CaptchaDetection.java

/***
 *  src  dst_0  dst_1/*from   www .  j ava  2 s  . c  o m*/
 * @param src
 * @param dst_0
 * @param dst_1 
 */
private static void delete_target(Mat src, Mat dst_0, Mat dst_1) {
    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            if (dst_0.get(row, col)[0] == 255 && dst_1.get(row, col)[0] == 255) {
                byte[] d_buff = new byte[1];
                d_buff[0] = (byte) 0;
                src.put(row, col, d_buff);
            }
        }
    }
}

From source file:opencv.CaptchaDetection.java

/***
 * /*from w w w.  j  a v  a 2s  .  co  m*/
 * @param src 
 */
private static void delete_point(Mat src) {
    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            if (src.get(row, col)[0] == 255) {
                int count = 0;

                if (row == 0 || src.get(row - 1, col)[0] == 0)
                    count++;
                if (row == src.rows() - 1 || src.get(row + 1, col)[0] == 0)
                    count++;
                if (col == 0 || src.get(row, col - 1)[0] == 0)
                    count++;
                if (col == src.cols() - 1 || src.get(row, col + 1)[0] == 0)
                    count++;

                if (count == 4) {
                    byte[] d_buff = new byte[1];
                    d_buff[0] = (byte) 0;
                    src.put(row, col, d_buff);
                }

            }
        }
    }
}

From source file:opencv.CaptchaDetection.java

/***
 * ?//from   www. j av a 2  s .  com
 * @param src
 * @param dst
 * @param degree 
 */
private static void rotateImage(Mat src, Mat dst, int degree) {
    Point center = new Point(src.cols() / 2.0 + 0.5, src.rows() / 2.0 + 0.5);
    Mat M = Imgproc.getRotationMatrix2D(center, degree, 1.0);
    Imgproc.warpAffine(src, dst, M, src.size());
}

From source file:opencv.CaptchaDetection.java

/***
 * ?//from ww  w .  j a  v  a  2s  . c om
 * @param src
 * @return 
 */
private static String dect_number(List<Mat> src) {
    String answer = "";

    for (Mat numRoi : src) {
        Mat zoomNum = new Mat(numRoi.rows() * 2, numRoi.cols() * 2, CvType.CV_8UC1, new Scalar(0));
        numRoi.copyTo(
                zoomNum.submat(new Rect(numRoi.cols() / 2, numRoi.rows() / 2, numRoi.cols(), numRoi.rows())));

        double matchMin = Double.MAX_VALUE;
        int matchSample = 0;

        for (Map.Entry<Integer, List<Mat>> iter : sampleMat.entrySet()) {
            for (Mat sample : iter.getValue()) {
                int result_cols = zoomNum.cols() - sample.cols() + 1;
                int result_rows = zoomNum.rows() - sample.rows() + 1;

                Mat resultImg = new Mat(result_rows, result_cols, CvType.CV_32FC1);

                Imgproc.matchTemplate(zoomNum, sample, resultImg, Imgproc.TM_SQDIFF);

                Core.MinMaxLocResult mmr = Core.minMaxLoc(resultImg);

                if (matchMin > mmr.minVal) {
                    matchMin = mmr.minVal;
                    matchSample = iter.getKey();
                }
            }
        }
        answer += matchSample / 2;
        //out.println("NumRio\tmatch sample :  " + matchSample + "\tmatch value : " + matchMin);
    }

    //out.println("Answer is : " + answer);
    return answer;
}

From source file:opencv.fark.FarkBulMainFrame.java

private static 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");
    g2.drawImage(matToBufImage.getBufferedImage(), 0, 0, null);

    Core.absdiff(gray1, gray2, sonuc);//ww  w.j a  v  a 2  s.co m
    Imgproc.blur(sonuc, sonuc, new Size(10, 10));
    Imgproc.threshold(sonuc, sonuc, 15, 255, Imgproc.THRESH_BINARY);
    Imgproc.erode(sonuc, sonuc, element);
    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++;
            }
        }
    }
    System.out.println();
    float hata = (sayac / boyut) * 100;
    System.out.println("Bozukluk oran % :" + hata);
    //        Imgproc.cvtColor(sonuc, sonuc, Imgproc.COLOR_GRAY2BGR);
    //        Core.bitwise_and(sonuc, tem, sonuc);

    matToBufImage.setMatrix(sonuc, ".jpg");
    g3.drawImage(matToBufImage.getBufferedImage(), 0, 0, null);
    //        Highgui.imwrite("D:\\farkResim.jpg",sonuc);
}

From source file:opencv.fark.ResimSecMainFrame.java

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);/*w  w  w  . j  a va2s .  co m*/
    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);

}

From source file:opencv.fark.ResimSecMainFrame.java

private void jButtonFarkBulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFarkBulActionPerformed
    farkli_pixel_say = 0;//from   w  w w.j ava2 s .c o m
    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();

}

From source file:opencv.fark.ResimSecMainFrame.java

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);/*from   ww  w  .  j a  v a2 s .c o m*/

        }

    }

    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);

}

From source file:opencv.fark.VideoSecMainFrame.java

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

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

            @Override//from ww  w .  j  a v a2  s . c o  m
            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!");
    }
}