public static int identify_R_U(Mat binary) { // 0: hull points identified < 2 // 1: R // 2: U List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); List<MatOfPoint> hullPoints = new ArrayList<MatOfPoint>(); List<Point> hullPointList = new ArrayList<Point>(); List<Point> filteredPointList = new ArrayList<Point>(); MatOfPoint hullPointMat = new MatOfPoint(); MatOfInt hull = new MatOfInt(); Mat hierarchy = new Mat(); // Find contour Imgproc.findContours( binary, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); // Find convex hull for (int k = 0; k < contours.size(); k++) { Imgproc.convexHull(contours.get(k), hull); for (int j = 0; j < hull.toList().size(); j++) { hullPointList.add(contours.get(k).toList().get(hull.toList().get(j))); } hullPointMat.fromList(hullPointList); hullPoints.add(hullPointMat); } // Filter hull points. Only hull points relevant to hull classification // will remain. // Also, count the number of filtered hull points in left and right. for (int l = 0; l < hullPointList.size(); l++) { if (hullPointList.get(l).y < binary.rows() * 0.25) { if (l != hullPointList.size() - 1) { if (Math.hypot( hullPointList.get(l).x - hullPointList.get(l + 1).x, hullPointList.get(l).y - hullPointList.get(l + 1).y) / binary.cols() > 0.08) { filteredPointList.add(hullPointList.get(l)); // Core.circle(rgb, // new Point(UL.x + hullPointList.get(l).x, UL.y // + hullPointList.get(l).y), 5, // new Scalar(0, 255, 0)); } } } } // Sort points by x axis, increasing if (filteredPointList.size() > 1) { if (filteredPointList.get(0).x > filteredPointList.get(1).x) { Point temp = filteredPointList.get(0); filteredPointList.set(0, filteredPointList.get(1)); filteredPointList.set(1, temp); } if (filteredPointList.get(0).y > filteredPointList.get(1).y) return 1; // R else return 2; // U } return 0; }