排序2d点数组以找出四个角 [英] Sorting 2d point array to find out four corners

查看:87
本文介绍了排序2d点数组以找出四个角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2d个点的集合,它们可以是任意大小.通过找到原点之间距离的最小值和最大值,我可以找出左上角和右下角的点,但我无法找出右上角和左下角.

Hi I have the collection of 2d points which can be of any size .By finding min and max value of the distance between origin I am able to find out the top left and bottom-right corner point but I am not able to find out top-right and bottom left point.

推荐答案

也许您可以使用cv::approxPoly()查找一组2D点的角.然后,您可以按照以下想要的任何顺序对点进行排序:

Perhaps you can use cv::approxPoly() to find the corners of your set of 2D points. Then you can sort the points in any order you want in the following way:

//Sorts a vector of 4 points into top left, top right, bottomleft, bottomright
vector<Point> sortPoints(vector<Point> unsorted) {
    vector<Point> sorted;
    for (int i = 0; i < 4; i++)sorted.push_back(Point(0, 0));
    int middleX = (unsorted[0].x + unsorted[1].x + unsorted[2].x + unsorted[3].x) / 4;
    int middleY = (unsorted[0].y + unsorted[1].y + unsorted[2].y + unsorted[3].y) / 4;
    for (int i = 0; i < unsorted.size(); i++) {
        if (unsorted.at(i).x < middleX && unsorted.at(i).y < middleY)sorted[0] = unsorted.at(i);
        if (unsorted.at(i).x > middleX && unsorted.at(i).y < middleY)sorted[1] = unsorted.at(i);
        if (unsorted.at(i).x < middleX && unsorted.at(i).y > middleY)sorted[2] = unsorted.at(i);
        if (unsorted.at(i).x > middleX && unsorted.at(i).y > middleY)sorted[3] = unsorted.at(i);
    }
    return sorted;
}

这篇关于排序2d点数组以找出四个角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆