|
|
|
| Am I sending you the right signals? |
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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;
}
});
}
|
|
#2
|
||||
|
||||
|
Re: Sort array in filterContours
FWIW you could also write that as
Code:
public static void sortContoursByArea(List<MatOfPoint> contours) {
Collections.sort(contours, Comparator.comparingDouble(Imgproc::contourArea));
}
|
|
#3
|
|||||
|
|||||
|
Re: Sort array in filterContours
Quote:
Is there a name for what is hapening with "Imgproc::contourArea" ? I'd like to look up the explanation of how that works... Clearly it's getting expanded internally into Imgprog.contourArea(contours[n]) for the comparison.... but I don't know how/why..... |
|
#4
|
||||
|
||||
|
Re: Sort array in filterContours
That's called a method reference.
This article explains it quite well Also, you don't even need a special method for sorting a list. The List interface defines a method "sort(Comparator)" that you can use inline like Code:
contours.sort(Comparator.comparingDouble(Imgproc::contourArea)); |
|
#5
|
|||||
|
|||||
|
Re: Sort array in filterContours
Quote:
|
|
#6
|
|||||
|
|||||
|
Re: Sort array in filterContours
Quote:
We were just trying to call the Collections.sort in-line. Your static method went in with no problems. I'll have to investigate further to understand why the static method worked, but the inline code didn't. Thanks again for the detailed response. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|