Ask Your Question
0

Arranging Contour Points in Clockwise order

asked 2018-09-25 10:12:47 -0600

balasubramaniyan94 gravatar image

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.

Please suggest.

edit retag flag offensive close merge delete

Comments

What about OpenGL?

sjhalayka gravatar imagesjhalayka ( 2018-09-26 09:37:28 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-09-25 12:41:41 -0600

updated 2018-09-26 00:40:41 -0600

berak gravatar image

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

edit flag offensive delete link more
0

answered 2020-02-07 23:59:43 -0600

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;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-09-25 10:12:47 -0600

Seen: 9,456 times

Last updated: Sep 26 '18