问题
I have a list that contains MatOfPoints which are nothing but contours that I detected from a image using opencv
.
How do I sort a List<MatOfPoints>
according to x
and y
coordinate of those contours?
I know that List
has a sort
method but I am confused by the Comparator
parameter.
回答1:
It depends how do you want to compare it, on of solution could be:
@Override
public int compare(MatOfPoints mop1, MatOfPoints mop2) {
long sumMop = 0;
long sumMop2 = 0;
for( Point p: mop1.toList() ){
sumMop1 += p.x + p.y
}
for( Point p: mop2.toList() ){
sumMop2 += p.x + p.y
}
if( sumMop1 > sumMop2)
return 1;
else if( sumMop1 < sumMop2 )
return -1;
else
return 0;
}
来源:https://stackoverflow.com/questions/45056866/how-to-sort-list-of-matofpoints-according-to-contour-x-y