# How to convert horizontal bounding box coordinates to oriented bounding box coordinates

I am trying to detect oriented bounding boxes with faster rcnn for a long time, but I could not make it to do so. I aim to detect objects in the DOTA dataset. I was using built-in faster rcnn model in pytorch, but realized that it does not support OBB. Then I found another library named detectron2 that is built on the pytorch framework. Built-in faster rcnn network in detectron2 is actually compatible with OBB but I could not make that model work with DOTA. Because I could not convert DOTA box annotations to (cx, cy, w, h, a). In DOTA, objects are annotated by coordinates of 4 corners which are (x1,y1,x2,y2,x3,y3,x4,y4).

I can't come up with a solution that converts these 4 coordinates to (cx, cy, w, h, a), where cx and cy are the center point of OBB and w, h, and a are width, height and angle respectively.

Is there any suggestion?

Assuming the rectangle is as below (easily adjusted if otherwise)

Then:

1. The tangent of the angle of rotation is given by

$$tan(a)=\frac{y_3-y_2}{x_3-x_2}$$

Depending on quadrant of interest one can take the opposite coordinates. Getting the inverse tan function gives the angle in radians.

1. The width is given by

$$w=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$

Similar for height

1. The center x is given by

$$cx=\frac{x_1+x_3}{2}$$

And similar for cy. Rotation is assumed to leave center unaffected.

• Thank you so much. It worked and I am surprised how simple these formulas are. Struggling too much prevents seeing simple solutions I think. Nov 17, 2021 at 7:10
• Your assumption is right. The location of the center point is not affected by the rotation angle of the rectangle. Nov 17, 2021 at 7:13
• Glad it helped. Visualizing the shape makes it easier. Nov 17, 2021 at 9:00
• Well, what if the starting point of the coordinates of the rectangle is not always started from the top left corner but from random corner and I always want to calculate the positive angle of the rectangle w.r.t x axis? How can you pick proper coordinates to calculate angle? Nov 17, 2021 at 12:45
• I think step 3 (in above answer by Nikos M.) should be... The center x is given by: $cx = \frac{x_1 + x_3}{2}$ Aug 11, 2022 at 14:20