List of usage examples for org.opencv.core Mat get
public double[] get(int row, int col)
From source file:opencv.CaptchaDetection.java
private static int col_length(Mat src, int dst_row, int dst_col) { int length = 0; for (int row = dst_row; row >= 0; row--) { if (src.get(row, dst_col)[0] == 255) length++;//w w w. j a v a2s . c om else break; } for (int row = dst_row + 1; row < src.rows(); row++) { if (src.get(row, dst_col)[0] == 255) length++; else break; } //out.println(length); return length; }
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 w w .ja v a2s . 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 a v a 2 s . co 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 ww . ja v a 2 s . c o 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.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 .java2s. c om*/ 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 av a 2 s.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 ww.j a v a 2 s .co 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.VideoSecMainFrame.java
private void jButtonKarsilastirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonKarsilastirActionPerformed if (referans_degeri == 1) { t1 = new Thread() { @Override/*from w w 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!"); } }
From source file:opencvdemos.BallGame.java
License:Apache License
private Mat findAndDrawObjects(Mat maskedImage, Mat frame) { // Init/*from w ww. java 2 s . com*/ List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); // Find contours Imgproc.findContours(maskedImage, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE); // If any contour exist... if (hierarchy.size().height > 0 && hierarchy.size().width > 0) { // for each contour, display it in blue for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) { Imgproc.drawContours(frame, contours, idx, new Scalar(250, 0, 0)); } } return frame; }
From source file:opencvtesting.OpenCVTesting.java
/** * @param args the command line arguments *//* w w w . j a v a 2s . c o m*/ public static void main(String[] args) throws IOException { /* 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(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ CameraWindow cWindow = new CameraWindow(); cWindow.setVisible(true); Random gen = new Random(); JFrame frameF = new JFrame(); System.loadLibrary("opencv_java248"); VideoCapture camera = new VideoCapture(0); camera.open(0); //Useless if (!camera.isOpened()) { System.out.println("Camera Error"); } else { System.out.println("Camera OK?"); } frame = new Mat(); frame_gray = new Mat(); camera.read(frame); showResult(frame, frameF); dst = new Mat(frame.size(), frame.type()); mapX = new Mat(frame.size(), CvType.CV_32FC1); mapY = new Mat(frame.size(), CvType.CV_32FC1); boolean bEnd = true; while (bEnd) { camera.read(frame); Imgproc.cvtColor(frame, frame_gray, Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(frame_gray, frame_gray, new Size(1, 1), 2); Imgproc.equalizeHist(frame_gray, frame_gray); Mat circles = new Mat(); Imgproc.HoughCircles(frame_gray, circles, Imgproc.CV_HOUGH_GRADIENT, cWindow.get_dp(), frame_gray.rows() / 8, cWindow.get_param1(), cWindow.get_param2(), 0, 0); //System.out.println(circles.rows()); for (int i = 0; i < circles.cols(); i++) { double[] circle = circles.get(0, i); Point center = new Point(Math.round(circle[0]), Math.round(circle[1])); int radius = (int) Math.round(circle[2]); Core.circle(frame, center, 3, new Scalar(gen.nextInt(), gen.nextInt(), gen.nextInt()), -1, 8, 0); Core.circle(frame, center, radius, new Scalar(gen.nextInt(), gen.nextInt(), gen.nextInt()), 3, 8, 0); } showResult(frame, frameF); } //Highgui.imwrite("camera1.jpg", frame); //System.out.println("OK"); }