Skip to content Skip to sidebar Skip to footer

How To Sort List Of Matofpoints According To Contour (x,y)

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 according to x and y coord

Solution 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;
}

Post a Comment for "How To Sort List Of Matofpoints According To Contour (x,y)"