OpenCV C++: Sorting contours by their contourArea

前端 未结 2 1367
迷失自我
迷失自我 2020-12-08 17:41

How can I sort contours by the size of their contour areas? And how can I get the biggest/smallest one?

2条回答
  •  -上瘾入骨i
    2020-12-08 18:00

    Just give a solution using the lambda function if C++11 is available.

        sort(contours.begin(), contours.end(), [](const vector& c1, const vector& c2){
        return contourArea(c1, false) < contourArea(c2, false);
    });
    

    Then you can access contours[0] to get the contour with the smallest area and contours[contours.size()-1] to get the one with the largest area because the contours are sorted in ascending order.

提交回复
热议问题