How to sort List of MatOfPoints according to contour (x,y)

混江龙づ霸主 提交于 2019-12-12 06:45:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!