对2d点阵列进行排序以找出四个角

时间:2014-07-31 05:55:29

标签: sorting opencv

嗨我有2d点的集合,可以是任何大小。通过查找原点之间距离的最小值和最大值,我能够找到左上角和右下角但我无法找出右上角和左下角。

2 个答案:

答案 0 :(得分:4)

也许您可以使用cv::approxPoly()查找2D点集的角落。然后,您可以按以下方式按任意顺序对点进行排序:

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

答案 1 :(得分:0)

p1(左,上),p2(右,下) - 你知道。

现在获取值top,left,bottom和right并形成其他点:

p3(右,上),第4页(左,下)。

相关问题