1

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.

Rahul galgali
  • 775
  • 2
  • 10
  • 26
  • use `boundingRect` http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=boundingrect#boundingrect and compute the top right and bottom left according to the shortest distance to the bounding rect corners. Though I think your method (even for top left and bottom right points) might fail for some simple examples and so my way would fail in the same way ;) . – Micka Jul 31 '14 at 08:13

2 Answers2

4

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;
}
diip_thomas
  • 1,531
  • 13
  • 25
0

You have this:

p1(left,top), p2(right,bottom).

Now get values top,left, bottom and right and form other points:

p3(right,top), p4(left,bottom).

stateMachine
  • 5,227
  • 4
  • 13
  • 29
Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42