View Single Post
  #2   Spotlight this post!  
Unread 07-02-2017, 06:58
pblankenbaker pblankenbaker is offline
Registered User
FRC #0868
 
Join Date: Feb 2012
Location: Carmel, IN, USA
Posts: 124
pblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of lightpblankenbaker is a glorious beacon of light
Re: Sort array in filterContours

When sorting objects with the Collections.sort() method, you will typically want to supply the "comparator" parameter that lets you control the comparison between each object (decide which should come before the other).

For example, if you want to sort by area, I think you can use a static helper method something like the following:

Code:
  public static void sortContoursByArea(List<MatOfPoint> contours) {
    Collections.sort(contours, new Comparator<MatOfPoint>() {

      @Override
      public int compare(MatOfPoint a, MatOfPoint b) {
        // Not sure how expensive this will be computationally as the
        // area is computed for each comparison
        double areaA = Imgproc.contourArea(a);
        double areaB = Imgproc.contourArea(b);

        // Change sign depending on whether your want sorted small to big
        // or big to small
        if (areaA < areaB) {
          return -1;
        } else if (areaA > areaB) {
          return 1;
        }
        return 0;
      }
    });
  }
Hope that helps
Reply With Quote