# Arranging Contour Points in Clockwise order

Hi to everyone.

I would like to know possibilities of sorting the single contour points in the clockwise order based on the center of the contour.

edit retag close merge delete

( 2018-09-26 09:37:28 -0600 )edit

Sort by ยป oldest newest most voted
MatOfPoint2f orderPointsClockwise(MatOfPoint2f screenCnt2f) {
System.out.println(screenCnt2f.dump());

List<Point> points = screenCnt2f.toList();
// # initialize a list of coordinates that will be ordered
// # such that the first entry in the list is the top-left,
// # the second entry is the top-right, the third is the
// # bottom-right, and the fourth is the bottom-left
Collections.sort(points, new Comparator<Point>() {
@Override
public int compare(Point p1, Point p2) {
double s1 = p1.x + p1.y;
double s2 = p2.x + p2.y;
return Double.compare(s1, s2);
}
});
Point topLeft = points.get(0);
Point bottomRight = points.get(3);

// # now, compute the difference between the points, the
// # top-right point will have the smallest difference,
// # whereas the bottom-left will have the largest difference
Collections.sort(points, new Comparator<Point>() {
@Override
public int compare(Point p1, Point p2) {
double s1 = p1.y - p1.x  ;
double s2 = p2.y - p2.x;
return Double.compare(s1, s2);
}
});
Point topRight = points.get(0);
Point bottomLeft = points.get(3);

Point[] pts = new Point[]{topLeft,topRight, bottomRight, bottomLeft};

screenCnt2f = new MatOfPoint2f(pts);
// System.out.println(screenCnt2f.dump());
return screenCnt2f;
}

more

I will simply guide you to the resources to find your solution.

First, familiarise yourself with the return orientation of findContours

Then this blog post explains how to do a clockwise order of points

more

Official site

GitHub

Wiki

Documentation