Knowing the center point, length, width and rotation angle, find the coordinates of the four vertices of the rectangle (Python)

Knowing the center point, length, width and rotation angle, find the coordinates of the four vertices of the rectangle

  • theoretical basis
    • Case 1:

      θ

      [

      0

      ,

      π

      /

      2

      ]

      \theta \in [0, \pi/2]

      θ∈[0,π/2]

    • Case two:

      θ

      [

      π

      /

      2

      ,

      π

      ]

      \theta \in [\pi/2,\pi]

      θ∈[π/2,π]

  • Python code implementation

Theoretical basis

There are several prerequisites for this implementation:

  • The known information is as follows: [x_center, y_center, w, h, angle], where the default

    w

    w

    w is the longest side of the rectangle, i.e.

    w

    >

    h

    w > h

    w>h.

  • known rotation angle

    θ

    \theta

    θ is the longest side of the rectangle

    w

    w

    w relative to

    x

    x

    The rotation angle of the x-coordinate axis

  • Rotation angle

    θ

    \theta

    The rotation interval of θ is in

    [

    0

    ,

    π

    ]

    [0, \pi]

    [0,π]

The situation can be divided into two types, namely

θ

[

0

,

π

/

2

]

\theta \in [0, \pi/2]

θ∈[0,π/2] and

θ

[

π

/

2

,

π

]

\theta \in [\pi/2, \pi]

θ∈[π/2,π]

Situation 1:

θ

[

0

,

π

/

2

]

\theta \in [0, \pi/2]

θ∈[0,π/2]

Look at the first case first

θ

[

0

,

π

/

2

]

\theta \in [0, \pi/2]

θ∈[0,π/2]:

The center point of the known rectangle

(

x

,

the y

)

(x, y)

(x,y), rotation angle

θ

\theta

θ is marked in orange in the figure.

come first

(

x

1

,

the y

1

)

(x_1,y_1)

(x1?,y1?), the green auxiliary line in the figure is used, and the angles of the triangles used are marked

θ

\theta

θ:

  • x

    1

    =

    x

    +

    c

    o

    the s

    θ

    ?

    w

    /

    2

    ?

    the s

    i

    no

    θ

    ?

    h

    /

    2

    x_1=x + cos\theta*w/2-sin\theta*h/2

    x1?=x + cosθ?w/2?sinθ?h/2

  • the y

    1

    =

    the y

    +

    the s

    i

    no

    θ

    ?

    w

    /

    2

    +

    c

    o

    the s

    θ

    ?

    h

    /

    2

    y_1=y + sin\theta*w/2 + cos\theta*h/2

    y1?=y + sinθ?w/2 + cosθ?h/2

ask again

(

x

2

,

the y

2

)

(x_2,y_2)

(x2?,y2?), using the purple auxiliary line in the figure:

  • x

    2

    =

    x

    +

    c

    o

    the s

    θ

    ?

    w

    /

    2

    +

    the s

    i

    no

    θ

    ?

    h

    /

    2

    x_2=x + cos\theta*w/2 + sin\theta*h/2

    x2?=x + cosθ?w/2 + sinθ?h/2

  • the y

    2

    =

    the y

    +

    the s

    i

    no

    θ

    ?

    w

    /

    2

    ?

    c

    o

    the s

    θ

    ?

    h

    /

    2

    y_2=y + sin\theta*w/2-cos\theta*h/2

    y2?=y + sinθ?w/2?cosθ?h/2

Next

(

x

3

,

the y

3

)

(x_3,y_3)

(x3?,y3?) and

(

x

4

,

the y

4

)

(x_4,y_4)

(x4?,y4?) is

(

x

1

,

the y

1

)

(x_1,y_1)

(x1?,y1?) and

(

x

2

,

the y

2

)

(x_2,y_2)

(x2?,y2?) about

(

x

,

the y

)

(x, y)

The symmetry point of (x, y) only needs to change the positive term into a negative term, and the negative term into a positive term:

  • x

    3

    =

    x

    ?

    c

    o

    the s

    θ

    ?

    w

    /

    2

    +

    the s

    i

    no

    θ

    ?

    h

    /

    2

    x_3=x-cos\theta*w/2 + sin\theta*h/2

    x3?=x?cosθ?w/2 + sinθ?h/2

  • the y

    3

    =

    the y

    ?

    the s

    i

    no

    θ

    ?

    w

    /

    2

    ?

    c

    o

    the s

    θ

    ?

    h

    /

    2

    y_3=y-sin\theta*w/2-cos\theta*h/2

    y3?=y?sinθ?w/2?cosθ?h/2

  • x

    4

    =

    x

    ?

    c

    o

    the s

    θ

    ?

    w

    /

    2

    ?

    the s

    i

    no

    θ

    ?

    h

    /

    2

    x_4=x-cos\theta*w/2-sin\theta*h/2

    x4?=x?cosθ?w/2?sinθ?h/2

  • the y

    4

    =

    the y

    ?

    the s

    i

    no

    θ

    ?

    w

    /

    2

    +

    c

    o

    the s

    θ

    ?

    h

    /

    2

    y_4=y-sin\theta*w/2 + cos\theta*h/2

    y4?=y?sinθ?w/2 + cosθ?h/2

Situation 2:

θ

[

π

/

2

,

π

]

\theta \in [\pi/2,\pi]

θ∈[π/2,π]


Similarly, the rotation angle

θ

\theta

θ is greater than

π

/

2

\pi/2

π/2, so the angles of the auxiliary triangles used are marked as

π

?

θ

\pi -\theta

π?θ.

come first

(

x

1

,

the y

1

)

(x_1,y_1)

(x1?,y1?), the green auxiliary line in the figure is used, and the angles of the triangles used are marked

π

?

θ

\pi -\theta

π?θ:

  • x

    1

    =

    x

    ?

    c

    o

    the s

    (

    π

    ?

    θ

    )

    ?

    w

    /

    2

    +

    the s

    i

    no

    (

    π

    ?

    θ

    )

    ?

    h

    /

    2

    =

    x

    +

    c

    o

    the s

    θ

    ?

    w

    /

    2

    +

    the s

    i

    no

    θ

    ?

    h

    /

    2

    x_1=x-cos(\pi-\theta)*w/2 + sin(\pi-\theta)*h/2=x + cos\theta*w/2 + sin\theta* h/2

    x1?=x?cos(π?θ)?w/2 + sin(π?θ)?h/2=x + cosθ?w/2 + sinθ?h/2

  • the y

    1

    =

    the y

    ?

    the s

    i

    no

    (

    π

    ?

    θ

    )

    ?

    w

    /

    2

    ?

    c

    o

    the s

    (

    π

    ?

    θ

    )

    ?

    h

    /

    2

    =

    the y

    ?

    the s

    i

    no

    θ

    ?

    w

    /

    2

    +

    c

    o

    the s

    θ

    ?

    h

    /

    2

    y_1=y-sin(\pi-\theta)*w/2-cos(\pi-\theta)*h/2=y-sin\theta*w/2 + cos\theta* h/2

    y1?=y?sin(π?θ)?w/2?cos(π?θ)?h/2=y?sinθ?w/2 + cosθ?h/2

  • x

    2

    =

    x

    ?

    c

    o

    the s

    (

    π

    ?

    θ

    )

    ?

    w

    /

    2

    ?

    the s

    i

    no

    (

    π

    ?

    θ

    )

    ?

    h

    /

    2

    =

    x

    +

    c

    o

    the s

    θ

    ?

    w

    /

    2

    ?

    the s

    i

    no

    θ

    ?

    h

    /

    2

    x_2=x-cos(\pi-\theta)*w/2-sin(\pi-\theta)*h/2=x + cos\theta*w/2-sin\theta* h/2

    x2?=x?cos(π?θ)?w/2?sin(π?θ)?h/2=x + cosθ?w/2?sinθ?h/2

  • the y

    2

    =

    the y

    +

    the s

    i

    no

    (

    π

    ?

    θ

    )

    ?

    w

    /

    2

    ?

    c

    o

    the s

    (

    π

    ?

    θ

    )

    ?

    h

    /

    2

    =

    the y

    +

    the s

    i

    no

    θ

    ?

    w

    /

    2

    +

    c

    o

    the s

    θ

    ?

    h

    /

    2

    y_2=y + sin(\pi-\theta)*w/2-cos(\pi-\theta)*h/2=y + sin\theta*w/2 + cos\theta* h/2

    y2?=y + sin(π?θ)?w/2?cos(π?θ)?h/2=y + sinθ?w/2 + cosθ?h/2

(

x

3

,

the y

3

)

(x_3,y_3)

(x3?,y3?) and

(

x

4

,

the y

4

)

(x_4,y_4)

(x4?,y4?) is

(

x

1

,

the y

1

)

(x_1,y_1)

(x1?,y1?) and

(

x

2

,

the y

2

)

(x_2,y_2)

(x2?,y2?) about

(

x

,

the y

)

(x, y)

The symmetry point of (x,y).

It can be seen that in the two cases, the obtained values of the four points are the same, for example, in case 1

(

x

1

,

the y

1

)

(x_1,y_1)

(x1?,y1?) and case two

(

x

2

,

the y

2

)

(x_2,y_2)

(x2?, y2?) are the same, so it can be discussed regardless of the situation in the code implementation, without affecting the final result.

python code implementation

def get_corners(box): #Here my project yaw [-pi/4, 3*pi/4), needs to be mapped to [0, pi)
    box = box.detach().cpu().numpy()
    x = box[0]
    y = box[1]
    w = box[2]
    l = box[3]
    yaw = box[4]
    if yaw <0: #used for mapping
        yaw = yaw + np.pi

    bev_corners = np.zeros((4, 2), dtype=np.float32)
    cos_yaw = np.cos(yaw)
    sin_yaw = np. sin(yaw)
 
    bev_corners[0, 0] = (w / 2) * cos_yaw - (l / 2) * sin_yaw + x
    bev_corners[0, 1] = (w / 2)* sin_yaw + (l / 2) * cos_yaw + y

    bev_corners[1, 0] = (l / 2) * sin_yaw + (w / 2) * cos_yaw + x
    bev_corners[1, 1] = (w / 2)* sin_yaw - (l / 2) * cos_yaw + y

    bev_corners[2, 0] = (-w / 2) * cos_yaw - (-l / 2) * sin_yaw + x
    bev_corners[2, 1] = (-w / 2)* sin_yaw + (-l / 2) * cos_yaw + y

    bev_corners[3, 0] = (-l / 2) * sin_yaw + (-w / 2) * cos_yaw + x
    bev_corners[3, 1] = (-w / 2)* sin_yaw - (-l / 2) * cos_yaw + y

    return bev_corners