private List<MatOfPoint> elementToPoints(Element element) { List<Point> points = new ArrayList<>(); points.add(new Point(element.x, element.y)); points.add(new Point(element.x + element.w, element.y)); points.add(new Point(element.x + element.w, element.y + element.h)); points.add(new Point(element.x, element.y + element.h)); MatOfPoint mPoints = new MatOfPoint(); mPoints.fromList(points); List<MatOfPoint> finalPoints = new ArrayList<MatOfPoint>(); finalPoints.add(mPoints); return finalPoints; }
MatOfPoint mop = new MatOfPoint(); mop.fromList(contour); Moments moments = Imgproc.moments(mop); Point centroid = new Point(); centroid.x = moments.get_m10() / moments.get_m00(); centroid.y = moments.get_m01() / moments.get_m00();
MatOfPoint sourceMat = new MatOfPoint(); sourceMat.fromList(pointsOrdered); //your sourceMat is Ready.
public FluentCv getContourMaxRects(List<MatOfPoint> contours, List<RotatedRect> rect) { List<Point> contoursCombined = new ArrayList<>(); for (MatOfPoint mp : contours) { List<Point> points = new ArrayList<>(); Converters.Mat_to_vector_Point(mp, points); for (Point point : points) { contoursCombined.add(point); } } contours.clear(); MatOfPoint points = new MatOfPoint(); points.fromList(contoursCombined); return getContourRects(Collections.singletonList(points), rect); }
public class YourJavaWrapper { static { System.loadLibrary("yourlibrary"); } public static MatOfPoint findMostFencyMatOfPoints(List<MatOfPoint> contours){ List<Mat> contoursTmp = new ArrayList<Mat>(contours.size()); Mat inputMat = Converters.vector_vector_Point_to_Mat(contours, contoursTmp); Mat outputMat = new Mat(); findMostFencyMatOfPoints(inputMat.nativeObj, outputMat.nativeObj); List<Point> pointsTmp = new ArrayList<Point>(); Converters.Mat_to_vector_Point(outputMat, pointsTmp); MatOfPoint matOfInterest = new MatOfPoint(); matOfInterest.fromList(pointsTmp); outputMat.release(); return matOfInterest; } private static native void findMostFencyMatOfPoints(long inputMatAddress, long outPutMatAddress); }
List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); MatOfInt4 hierarchy = new MatOfInt4(); MatOfInt hull = new MatOfInt(); void foo(Mat gray) { Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); for (int i = 0; i < contours.size(); i++) { Imgproc.convexHull(contours.get(i), hull); MatOfPoint hullContour = hull2Points(hull, contours.get(i)); Rect box = Imgproc.boundingRect(hullContour); Mat hullMat = new Mat(gray, box); ... } } MatOfPoint hull2Points(MatOfInt hull, MatOfPoint contour) { List<Integer> indexes = hull.toList(); List<Point> points = new ArrayList<>(); MatOfPoint point= new MatOfPoint(); for(Integer index:indexes) { points.add(contour.toList().get(index)); } point.fromList(points); return point; }