I got this error to compare two contourAreas in openCV project.
java.lang.IllegalArgumentException: Comparison method violates its general contract!
And I've tried:
Collections.sort(contours) { o1, o2 ->
val area1 = Imgproc.contourArea(o1)
val area2 = Imgproc.contourArea(o2)
(area2 - area1).toInt()
}
contours.sortWith(Comparator { o1, o2 ->
val area1 = Imgproc.contourArea(o1)
val area2 = Imgproc.contourArea(o2)
(area2 - area1).toInt()
})
And I thought those above are dubious so I tried:
contours.sortWith(Comparator { o1, o2 ->
val area1 = Imgproc.contourArea(o1)
val area2 = Imgproc.contourArea(o2)
val res = (area2 - area1)
if (res > 0) {
return@Comparator 1
} else if(res < 0){
return@Comparator -1
}
return@Comparator 0
})
But none of them worked properly and my app crushed. How can I solve this problem?