List of usage examples for org.opencv.imgproc Imgproc findContours
public static void findContours(Mat image, List<MatOfPoint> contours, Mat hierarchy, int mode, int method, Point offset)
From source file:com.example.sarthuak.opencv.MainActivity.java
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { // TODO Auto-generated method stub final int viewMode = mViewMode; switch (viewMode) { case VIEW_MODE_RGBA: // input frame has RBGA format mRgba = inputFrame.rgba();/*from w ww.jav a2 s .c o m*/ break; case VIEW_MODE_CANNY: // input frame has gray scale format mRgba = inputFrame.rgba(); Imgproc.Canny(inputFrame.gray(), mRgbaF, 80, 100); Imgproc.cvtColor(mRgbaF, mRgba, Imgproc.COLOR_GRAY2RGBA, 4); break; case VIEW_MODE_ocr: startActivity(new Intent(this, ScanLicensePlateActivity.class)); break; case VIEW_MODE_new: Mat mRgba; mRgba = inputFrame.rgba(); drawing = mRgba.clone(); mRgbaT = drawing; Imgproc.cvtColor(drawing, mRgbaT, Imgproc.COLOR_BGR2GRAY); org.opencv.core.Size s = new Size(1, 1); Imgproc.GaussianBlur(mRgbaT, mRgbaT, s, 0, 0); Imgproc.Canny(mRgbaT, mRgbaT, 100, 255); Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5)); Imgproc.dilate(mRgbaT, mRgbaT, element); List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(drawing, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); double maxArea = -1; int maxAreaIdx = -1; for (int idx = 0; idx < contours.size(); idx++) { Mat contour = contours.get(idx); double contourarea = Imgproc.contourArea(contour); if (contourarea > maxArea) { maxArea = contourarea; maxAreaIdx = idx; } } Imgproc.drawContours(mRgba, contours, maxAreaIdx, new Scalar(255, 0, 0), 5); } return mRgba; // This function must return }
From source file:com.opencv.mouse.MouseMainFrame.java
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed try {// w w w .j av a 2 s . c om robot = new Robot(); } catch (AWTException e) { } t = new Thread() { public void run() { MatToBufImg matToBufferedImageConverter = new MatToBufImg(); //Utility class to convert Mat to Java's BufferedImage webCam = new VideoCapture(0); if (!webCam.isOpened()) { System.out.println("Kamera Ak Deil..!"); } else System.out.println("Kamera Ald --> " + webCam.toString()); Mat webcam_image = new Mat(480, 640, CvType.CV_8UC3); Mat hsv_image = new Mat(webcam_image.cols(), webcam_image.rows(), CvType.CV_8UC3); thresholded = new Mat(webcam_image.cols(), webcam_image.rows(), CvType.CV_8UC3, new Scalar(255, 255, 255)); if (webCam.isOpened()) { try { Thread.sleep(1000); } catch (InterruptedException ex) { } while (true) { try { webCam.read(webcam_image); } catch (Exception e) { System.out.println("Web Cam Kapal !"); } if (!webcam_image.empty()) { try { Thread.sleep(10); } catch (InterruptedException ex) { } // Mat inRangeResim = webcam_image.clone(); /* Mat inRangeResim = webcam_image.clone(); matToBufferedImageConverter.setMatrix(inRangeResim, ".jpg"); image =matToBufferedImageConverter.getBufferedImage(); Highgui.imwrite("D:\\bitirme.jpg", inRangeResim); */ // MatOfRect faceDetections = new MatOfRect(); Imgproc.cvtColor(webcam_image, hsv_image, Imgproc.COLOR_BGR2HSV); //siyah hsv range 0 0 0 - 180 45 100 //hsvmavi Core.inRange(webcam_image, new Scalar(75,63,40), new Scalar(118,255,255), webcam_image); //rgb mavi // Core.inRange(webcam_image, new Scalar(50,0,0), new Scalar(255,0,0), webcam_image); //turuncu hsv Core.inRange(webcam_image, new Scalar(5,50,50), new Scalar(15,255,255), webcam_image); //Core.inRange(webcam_image, new Scalar(80,50,50), new Scalar(140,255,255), webcam_image); // Core.inRange(webcam_image, new Scalar(29,0,24), new Scalar(30,155,155), webcam_image); //hsv mavi // jSliderHmin.setValue(75); // jSliderSmin.setValue(63); // jSliderVmin.setValue(40); // jSliderHmax.setValue(118); // jSliderSmax.setValue(255); // jSliderVmax.setValue(255); // // jSliderHmin.setValue(0); // jSliderSmin.setValue(0); // jSliderVmin.setValue(0); // jSliderHmax.setValue(179); // jSliderSmax.setValue(39); // jSliderVmax.setValue(120); Core.inRange(hsv_image, new Scalar(100, 97, 206), new Scalar(120, 255, 255), thresholded); Imgproc.dilate(thresholded, thresholded, element); Imgproc.erode(thresholded, thresholded, element); Imgproc.dilate(thresholded, thresholded, element); Imgproc.erode(thresholded, thresholded, element); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Imgproc.drawContours(thresholded, contours, -1, new Scalar(255.0, 255.0, 255.0), 5); 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 = thresholded.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(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); //} //} if (rect.height > 15 && rect.width > 15) { System.out.println(rect.x + "\n" + rect.y); Core.circle(webcam_image, new Point(rect.x, rect.y), i, new Scalar(0, 255, 0)); robot.mouseMove((int) (rect.x * 3), (int) (rect.y * 2.25)); } } // Imgproc.cvtColor(webcam_image, webcam_image, Imgproc.COLOR_HSV2BGR); // hsv_image.convertTo(hsv_image, CvType.CV_32F); // Imgproc.Canny(thresholded, thresholded, 10, 20); // Core.bitwise_and(thresholded, webcam_image, webcam_image); //ise yarar // Imgproc.cvtColor(thresholded, thresholded, Imgproc.COLOR_GRAY2BGR); // Core.bitwise_and(thresholded, webcam_image, webcam_image); // webcam_image.copyTo(hsv_image, thresholded); // System.out.println("<------------------------------>"); // System.out.println("BGR: " +webcam_image.channels()+" Size : "+webcam_image.size()); // System.out.println("HSV :"+hsv_image.channels()+" Size: "+hsv_image.size()); // System.out.println("Thresold :"+thresholded.channels()+" Size : "+thresholded.size()); // System.out.println("<------------------------------>"); // matToBufferedImageConverter.setMatrix(webcam_image, ".jpg"); image = matToBufferedImageConverter.getBufferedImage(); g.drawImage(image, 0, 0, webcam_image.cols(), webcam_image.rows(), null); } else { System.out.println("Grnt yok!"); break; } } // webCam.release(); } } }; threadDurum = true; t.start(); }
From source file:com.opencv.video.VideoCaptureMain.java
private void jButtonPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPlayActionPerformed Thread t = new Thread() { @Override/* ww w .ja v a 2 s .c o m*/ public void run() { MatToBufImg matToBufferedImageConverter = new MatToBufImg(); try { final VideoCapture videoCapture = new VideoCapture("D:\\colorTest.mp4"); // videoCapture = new VideoCapture(0); // Thread.sleep(3000); if (!videoCapture.isOpened()) { System.out.println("Video Alamad"); return; } double fps = videoCapture.get(5); System.out.println("FPS :" + fps); frame = new Mat(); Mat hsv_image = new Mat(); Mat thresholded = new Mat(); while (true) { boolean basarili = videoCapture.read(frame); if (!basarili) { System.out.println("Okunamyor"); break; } 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, 640, 480, null); } } catch (Exception e) { System.out.println("Sorun Burda"); } } }; t.start(); }
From source file:detectiontest.ParticleDetector.java
/** * Particle detection algorithm./*w w w .ja v a 2s . com*/ * * @param image an image where we want to detect * @return list of detected particles */ public static List<Particle> detect(Mat image) { // blur the image to denoise Imgproc.blur(image, image, new Size(3, 3)); // thresholds the image Mat thresholded = new Mat(); Imgproc.threshold(image, thresholded, THRESHOLD, MAX, Imgproc.THRESH_TOZERO_INV); // detect contours List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); // create particle from each contour List<Particle> particles = new ArrayList<>(); for (MatOfPoint contour : contours) { particles.add(new Particle(contour)); } return particles; }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
/** * Finds the bounding box for the book on the stand using * the high resolution image./*from w w w.java 2 s . c om*/ * @param src- High Resolution image of the book * @return Rectangle delineating the book */ public Rect highRes(Mat src) { Mat dst = src.clone(); Imgproc.blur(src, dst, new Size(100.0, 100.0), new Point(-1, -1), Core.BORDER_REPLICATE); Imgproc.threshold(dst, dst, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU); Imgproc.Canny(dst, dst, 50, 200, 3, false); List<MatOfPoint> contours = new LinkedList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Mat color = new Mat(); Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR); for (int k = 0; k < contours.size(); k++) { byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size()); Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 8); } new IViewer("HighRes Contours ", BReaderTools.bufferedImageFromMat(color)); Point center = new Point(src.cols() / 2, src.rows() / 2); //Check hierarchy tree int[] res = polySearch(center, hierarchy, contours, 0); while (res[0] != 1 && res[2] != -1) { res = polySearch(center, hierarchy, contours, res[2]); if (res[0] == 1) break; } MatOfInt tHull = new MatOfInt(); int index = 0; if (res[1] != -1) { index = res[1]; } Imgproc.convexHull(contours.get(index), tHull); //get bounding box MatOfPoint cont = contours.get(index); Point[] points = new Point[tHull.rows()]; for (int i = 0; i < tHull.rows(); i++) { int pIndex = (int) tHull.get(i, 0)[0]; points[i] = new Point(cont.get(pIndex, 0)); } Rect out = Imgproc.boundingRect(new MatOfPoint(points)); return out; }
From source file:edu.fiu.cate.breader.BaseSegmentation.java
/** * Finds the bounding box for the book on the stand using * the depth average image./*from w w w . j a v a2s . c o m*/ * @param src- The Depth average image * @return Rectangle delineating the book */ public Rect lowResDist(Mat src) { Mat dst = src.clone(); Imgproc.blur(src, dst, new Size(5, 5), new Point(-1, -1), Core.BORDER_REPLICATE); // Imgproc.threshold(dst, dst, 0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU); Imgproc.Canny(dst, dst, 50, 200, 3, false); // Canny(src, dst, 20, 60, 3); List<MatOfPoint> contours = new LinkedList<>(); Mat hierarchy = new Mat(); /// Find contours Imgproc.findContours(dst, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Mat color = new Mat(); Imgproc.cvtColor(src, color, Imgproc.COLOR_GRAY2BGR); for (int k = 0; k < contours.size(); k++) { byte[] vals = ITools.getHeatMapColor((float) k / (float) contours.size()); Imgproc.drawContours(color, contours, k, new Scalar(vals[0], vals[1], vals[2]), 1); } new IViewer("LowRes Contours ", BReaderTools.bufferedImageFromMat(color)); for (int k = 0; k < contours.size(); k++) { MatOfPoint2f tMat = new MatOfPoint2f(); Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(k).toArray()), tMat, 5, true); contours.set(k, new MatOfPoint(tMat.toArray())); } List<Point> points = new LinkedList<Point>(); for (int i = 0; i < contours.size(); i++) { points.addAll(contours.get(i).toList()); } MatOfInt tHull = new MatOfInt(); Imgproc.convexHull(new MatOfPoint(points.toArray(new Point[points.size()])), tHull); //get bounding box Point[] tHullPoints = new Point[tHull.rows()]; for (int i = 0; i < tHull.rows(); i++) { int pIndex = (int) tHull.get(i, 0)[0]; tHullPoints[i] = points.get(pIndex); } Rect out = Imgproc.boundingRect(new MatOfPoint(tHullPoints)); return out; }
From source file:org.pattern.detection.contour.ContourDetectionAlgorithm.java
@Override public List<? extends Particle> detectAndAssign(ParticleImage image) { // take the copy of image that we dont modify the original Mat img = new Mat(); image.getPixels().copyTo(img);/* w w w. j a v a2 s . c o m*/ // blur the image to denoise //Imgproc.blur(imagePixels, imagePixels, new Size(3, 3)); // thresholds the image Mat thresholded = new Mat(); // Imgproc.threshold(imagePixels, thresholded, // THRESHOLD, MAX, Imgproc.THRESH_TOZERO_INV); // thresholding Imgproc.adaptiveThreshold(img, thresholded, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 155, 15); Highgui.imwrite("1_thresholded.jpg", thresholded); Mat edges = new Mat(); Imgproc.Canny(img, edges, 100, 200); Highgui.imwrite("1_canny.jpg", edges); // remove small noises // Mat kernel = Mat.ones(new Size(3, 3), CvType.CV_8UC1); Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(5, 5)); Imgproc.morphologyEx(thresholded, thresholded, Imgproc.MORPH_OPEN, kernel); Highgui.imwrite("2_opening.jpg", thresholded); // Imgproc.erode(thresholded, thresholded, kernel, ORIGIN, 3); // Highgui.imwrite("3_erode.jpg", thresholded); Mat distTransform = new Mat(); Imgproc.distanceTransform(thresholded, distTransform, Imgproc.CV_DIST_C, 5); distTransform.convertTo(distTransform, CvType.CV_8UC1); Imgproc.equalizeHist(distTransform, distTransform); Highgui.imwrite("4_distance_transform.jpg", distTransform); Mat markerMask = Mat.zeros(img.size(), CvType.CV_8UC1); double max = Core.minMaxLoc(distTransform).maxVal; Imgproc.threshold(distTransform, markerMask, max * 0.9, 255, Imgproc.THRESH_BINARY); markerMask.convertTo(markerMask, CvType.CV_8UC1); Highgui.imwrite("5_thresholded_distance.jpg", markerMask); List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(markerMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); Mat markers = Mat.zeros(img.size(), CvType.CV_32S); //markers.setTo(Scalar.all(0)); Random rand = new Random(); for (int idx = 0; idx < contours.size(); idx++) { Scalar color = new Scalar(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); Imgproc.drawContours(markers, contours, idx, color, -1); } Highgui.imwrite("6_markers.jpg", markers); Imgproc.cvtColor(img, img, Imgproc.COLOR_GRAY2RGB); img.convertTo(img, CvType.CV_8UC3); Imgproc.watershed(img, markers); Highgui.imwrite("7_wattershed.jpg", markers); // detect contours // List<MatOfPoint> contours = new ArrayList<>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, ORIGIN); // create particle from each contour List<Particle> particles = new ArrayList<>(); int i = 0; for (MatOfPoint contour : contours) { Point cog = calcCog(contour); if (!Double.isNaN(cog.x) && !Double.isNaN(cog.y)) { System.out.println(cog); Particle p = new Particle(cog, contour); particles.add(p); // just for reorting reasons image.assign(p); } } return particles; }