Convex Hull on Java Android Opencv 2.3

前端 未结 6 2109
走了就别回头了
走了就别回头了 2020-12-11 06:45

Please help me,

I have a problem for Convex Hull on Android. I use Java and OpenCV 2.3.

Before I made it on Java, I made it on C++ with Vis

6条回答
  •  余生分开走
    2020-12-11 07:24

    Example in Java (OpenCV 2.4.11)

    hullMat contains the sub mat of gray, as identified by the convexHull method.
    You may want to filter the contours you really need, for example based on their area.

    List contours = new ArrayList();
    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 indexes = hull.toList();
        List points = new ArrayList<>();
        MatOfPoint point= new MatOfPoint();
        for(Integer index:indexes) {
            points.add(contour.toList().get(index));
        }
        point.fromList(points);
        return point;
    }
    

提交回复
热议问题