List of usage examples for org.opencv.core MatOfInt rows
public int rows()
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 ww w . j a va 2 s . c o m*/ * @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 ww w .j av a2 s. com * @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.usfirst.frc.team2084.CMonster2016.vision.BallProcessor.java
License:Open Source License
@Override public void process(Mat cameraImage) { Imgproc.blur(cameraImage, hsvImage, new Size(20, 20)); Imgproc.cvtColor(hsvImage, hsvImage, Imgproc.COLOR_BGR2HSV); // Threshold image to find blue/green Core.inRange(hsvImage, thresholdMin, thresholdMax, thresholdImage); Imgproc.erode(thresholdImage, thresholdImage, KERNEL); Imgproc.dilate(thresholdImage, thresholdImage, KERNEL); thresholdImage.copyTo(contourImage); contours.clear();//from w ww .java 2s. c o m hulls.clear(); Imgproc.findContours(contourImage, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Imgproc.drawContours(cameraImage, contours, -1, CONTOUR_COLOR); for (int i = 0; i < contours.size(); i++) { MatOfPoint contour = contours.get(i); MatOfInt hullIndices = new MatOfInt(); MatOfPoint hull; hulls.add(hull = new MatOfPoint()); Imgproc.convexHull(contour, hullIndices); for (int c = 0; c < hullIndices.rows(); c++) { int v = (int) hullIndices.get(c, 0)[0]; hull.put(c, 0, contour.get(v, 0)); // System.out.println(v); } // System.out.println(hull.size()); } Imgproc.drawContours(cameraImage, hulls, -1, CONTOUR_COLOR); debugImage("Threshold Image", thresholdImage); // debugImage("Contour Image", contourImage); }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.BoulderProcessor.java
License:Open Source License
private static MatOfPoint convexHull(MatOfPoint contour) { MatOfInt hullMatrix = new MatOfInt(); Imgproc.convexHull(contour, hullMatrix); // perform convex hull, gap // filler MatOfPoint hull = new MatOfPoint(); hull.create(hullMatrix.rows(), 1, CvType.CV_32SC2); for (int r = 0; r < hullMatrix.rows(); r++) { hull.put(r, 0, contour.get((int) hullMatrix.get(r, 0)[0], 0)); }/* www . j a va 2 s .c o m*/ return hull; }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.HighGoalProcessor.java
License:Open Source License
private MatOfPoint convexHull(MatOfPoint contour) { MatOfInt hullMatrix = new MatOfInt(); Imgproc.convexHull(contour, hullMatrix); // perform convex hull, gap // filler MatOfPoint hull = new MatOfPoint(); hull.create(hullMatrix.rows(), 1, CvType.CV_32SC2); for (int r = 0; r < hullMatrix.rows(); r++) { hull.put(r, 0, contour.get((int) hullMatrix.get(r, 0)[0], 0)); }//from w w w . ja v a 2s. c o m return hull; }