簡體   English   中英

如何從ArrayList中獲取最大值 <MatOfPoint> ?

[英]How to get the largest value from ArrayList<MatOfPoint>?

首先,我正在嘗試在OpenCV Android中在矩陣上繪制輪廓和凸包。 我將這些代碼遵循到我的程序中,如下所示:

/// 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);

,但是我什么也打不下去。 任何建議都會有所幫助。 之前謝謝。

抱歉,為時已晚。

我認為您可以使用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為面積值。

您可以使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM