![]() |
Sort array in filterContours
Hi
New to Java.... In the Grip generated pipeline code, I want to sort the list of filtered contours to locate the best matches.... Assuming for the moment that I wanted to use area, can anyone show me a code snipit that I can add to the filterContours() method to sort the output array.... Seems like I need to use the Collections.sort() method but I can't get it to accept my arguments. Code:
/** |
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) { |
Re: Sort array in filterContours
FWIW you could also write that as
Code:
public static void sortContoursByArea(List<MatOfPoint> contours) { |
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. |
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..... |
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)); |
Re: Sort array in filterContours
Quote:
|
| All times are GMT -5. The time now is 15:34. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi