What is the best way to sort the corners of a rectangle without knowing the width or height?

I have four coordinates of the detected rectangle. I want to know which of them are upper left, upper right, lower left and lower right points. The method that I wrote myself (unnecessarily) for a long time and actually did not work (perhaps because I was mistaken somewhere). In any case, I'm sure there is an easier way, but I cannot find a method using Google. Therefore any help would be appreciated.

My method is to find angles with max y (most-top), min y (most-bottom), max x (rightmost), min x (leftmost). And then, if the most-left.y> the most-right.y, then the upper left point is the leftmost, the upper right is the highest, etc. Is this method correct? And is there an easier way?

My code, sorry for its messy and confusing ... squareX [0] is the first x coordinate. [0] is the first y coordinate, etc.

Edit: It turns out that my method is fine, it just max () does not do what I thought it did (return the maximum number of all entered numbers).

            maxx = max(squareX[0], squareX[1], squareX[2], squareX[3]);
            maxy = max(squareY[0], squareY[1], squareY[2], squareY[3]);
            minx = min(squareX[0], squareX[1], squareX[2], squareX[3]);
            miny = min(squareY[0], squareY[1], squareY[2], squareY[3]);

            if (squareX[0] == maxx)
            {
                mx = 0;
            }
            if (squareX[1] == maxx)
            {
                mx = 1;
            }
            if (squareX[2] == maxx)
            {
                mx = 2;
            }
            if (squareX[3] == maxx)
            {
                mx =3;
            }

            if (squareX[0] == minx)
            {
                mix = 0;
            }
            if (squareX[1] == minx)
            {
                mix = 1;
            }
            if (squareX[2] == minx)
            {
                mix = 2;
            }
            if (squareX[3] == minx)
            {
                mix = 3;
            }

            if (squareY[0] == maxy)
            {
                my = 0;
            }
            if (squareY[1] == maxy)
            {
                my = 1;
            }
            if (squareY[2] == maxy)
            {
                my = 2;
            }
            if (squareY[3] == maxy)
            {
                my = 3;
            }

            if (squareY[0] == miny)
            {
                miy = 0;
            }
            if (squareY[1] == miny)
            {
                miy = 1;
            }
            if (squareY[2] == miny)
            {
                miy = 2;
            }
            if (squareY[3] == miny)
            {
                miy = 3;
            }

            if (squareY[mix] > squareY[mx])
            {
                Pt1.x = squareX[mix];
                Pt1.y = squareY[mix];
                Pt2.x = squareX[my];
                Pt2.y = squareY[my];
                Pt3.x = squareX[mx];
                Pt3.y = squareY[mx];
                Pt4.x = squareX[miy];
                Pt4.y = squareY[miy];
            }
            else
            {
                Pt1.x = squareX[my];
                Pt1.y = squareY[my];
                Pt2.x = squareX[mx];
                Pt2.y = squareY[mx];
                Pt3.x = squareX[miy];
                Pt3.y = squareY[miy];
                Pt4.x = squareX[mix];
                Pt4.y = squareY[mix];
            }

thank

+4
source share
4 answers

You can use the standard C ++ library, min_elementand max_elementusing a special comparator. Here is the official documentation.

I have a simple example here (this is not purely opencv sorry, but porting is trivial).

+1
source

, . - . . , . .

+3

:

if X0 == X1:
  # P0 and P1 are on a vertical
  if X0 < X2:
    # P0 and P1 are on the left
  else:
    # P2 and P3 are on the left
    swap P0-P2
    swap P1-P3
else:
  # P0 and P1 are on a diagonal
  if X0 < X1:
    # P0 is on the left
    if X0 < X2:
      # P0 and P3 are on the left
      swap P1-P3
    else:
      # P0 and P2 are on the left
      swap P1-P2
  else:
    # P1 is on the left
    if X1 < X2:
      # P1 and P3 are on the left
      swap P0-P3
    else:
      # P1 and P2 are on the left
      swap P0-P2

X ( P0-P1 P2-P3).

if Y0 > Y1:
  # P1 is upper
  swap P0-P1

if Y2 > Y3:
  # P3 is upper
  swap P2-P3

Y ( ) 0231 .

5 , 24 (2 ^ 4 <= 24 <= 2 ^ 5). , , ( ), .

0

:

  • .
  • Use the center of your rectangle as a new source for your corners.
  • Convert angles to polar coordinates.
  • Sort angles by angle.
0
source

Source: https://habr.com/ru/post/1531620/


All Articles