【问题标题】:How to get the largest value from ArrayList<MatOfPoint>?如何从 ArrayList<MatOfPoint> 中获取最大值?
【发布时间】:2015-07-05 13:52:42
【问题描述】:

对于初学者,我正在尝试在 OpenCV Android 中的矩阵上绘制轮廓和凸包。我跟随these codes 进入我的程序,如下所示:

/// Start contourImg
    Log.i(TAG, "called contourImg");

//init
    List<MatOfInt> hull         = new ArrayList<MatOfInt>();
    List<Point[]> hullPoints    = new ArrayList<Point[]>();
    List<MatOfPoint> hullMOP    = new ArrayList<MatOfPoint>();
    List<MatOfPoint> contours   = new ArrayList<MatOfPoint>();
    Mat overlay     = input.clone();
    Mat hierarchy   = new Mat(mask.rows(), mask.cols(), CvType.CV_8UC1, new Scalar(0));
    Point titik1    = new Point(0,0);
            
    Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, titik1);
            
//Find the convex hull
    for (int i = 0; i < contours.size(); i++) {
        hull.add(new MatOfInt());
    }
    for (int i = 0; i < contours.size(); i++) {
        Imgproc.convexHull(contours.get(i), hull.get(i), false);
    }
            
// Convert MatOfInt to MatOfPoint for drawing convex hull
            
// Loop over all contours   
    for (int i = 0; i < contours.size(); i++) {
        Point[] points = new Point[hull.get(i).rows()];
                
    // Loop over all points that need to be hulled in current contour
        for (int j = 0; j < hull.get(i).rows(); j++) {
            int index = (int) hull.get(i).get(j,  0)[0];
            points[j] = new Point(contours.get(i).get(index, 0)[0], contours.get(i).get(index, 0)[1]);
        }
        hullPoints.add(points);
    }
            
// Convert Point arrays into MatOfPoint
    for (int i = 0; i < hullPoints.size(); i++) {
        MatOfPoint mop = new MatOfPoint();
        mop.fromArray(hullPoints.get(i));
        hullMOP.add(mop);
    }
            
// Draw contours + hull results
    for (int i = 0; i < contours.size(); i++) {
        Imgproc.drawContours(overlay, contours, i, green);
        Imgproc.drawContours(overlay, hullMOP, i, red); 
    }

基本上,输出将分别以绿色和红色绘制获得的轮廓和凸包。不幸的是,绘图变得太荒谬了,小轮廓和凸包也被绘制出来了。

编辑:这是图像示例。注意那些也出现的较小的红色和绿色区域,而我不希望它们出现。

问题是:我应该如何操作hullMOP 数组列表,以便获得指向MatOfPoint 的索引,并且只包含最大的轮廓和凸包?我试图通过尝试找到最大的区域(如下)来应用与Imgproc.boundingRect() 方法相同的想法:

for (int i = 0; i < contours.size(); i++) {
    boundRect[i] = Imgproc.boundingRect(polyMOP.get(i));
}
Rect bigRect = new Rect();
for (int i = 0; i < boundRect.length; i++) {
    if (bigRect.area() < boundRect[i].area()) {
        bigRect = boundRect[i];
    }
}
Core.rectangle(image3, bigRect.tl(), bigRect.br(), green, 2);

,但我没有受到任何打击。任何建议都会有所帮助。之前谢谢。

【问题讨论】:

    标签: java android opencv image-processing arraylist


    【解决方案1】:

    如果有点晚了,我很抱歉。

    我认为您可以使用Imgproc.contourArea 来检查hullMOPcontours 的最大面积。

    这是一个例子:

    Mat maxContour = null;
    double maxContourarea=0;
    for (int idx = 0; idx < contours.size(); idx++) {
        Mat contour = contours.get(idx);
        double contourarea = Imgproc.contourArea(contour);
        if (contourarea > maxContourarea){
            maxContourarea = contourarea;
            maxContour = contour;
        }
    }
    

    因此maxContour 将包含最大的轮廓,maxContourarea 是面积值。

    【讨论】:

      【解决方案2】:

      您可以使用Imgproc.RETR_EXTERNAL
      CV_RETR_EXTERNAL 仅检索极端外轮廓。它为所有轮廓设置hierarchy[i][2]=hierarchy[i][3]=-1。 换句话说,使用Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, titik1)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-23
        相关资源
        最近更新 更多