Json formats exported using Object Detection with Bounding Boxes include x,y,width,height,rotation. How do you calculate the coordinates of the remaining three points?
The document explains that the exported x,y is before the rotation, but the actual use of the output is found to be the rotation of x,y。
I want to calculate the coordinates of the other three points, here’s my code:
def convert_bbox_to_points(x, y, width, height, rotation, original_width, original_height):
# 计算矩形的中心点
center_x = x + width / 2
center_y = y + height / 2
Check this function for yolo obb, looks like you need the same:
def convert_annotation_to_yolo_obb(label, normalize=True):
"""
Convert LS annotation to Yolo OBB format.
Args:
label (dict): Dictionary containing annotation information including:
- original_width (int): Original width of the image.
- original_height (int): Original height of the image.
- x (float): X-coordinate of the top-left corner of the object in percentage of the original width.
- y (float): Y-coordinate of the top-left corner of the object in percentage of the original height.
- width (float): Width of the object in percentage of the original width.
- height (float): Height of the object in percentage of the original height.
- rotation (float, optional): Rotation angle of the object in degrees (default is 0).
normalize (bool, optional): Whether to normalize the coordinates to the range [0, 1] (default is True).
Returns:
list of tuple or None: List of tuples containing the coordinates of the object in Yolo OBB format.
Each tuple represents a corner of the bounding box in the order:
(top-left, top-right, bottom-right, bottom-left).
"""
if not (
"original_width" in label
and "original_height" in label
and "x" in label
and "y" in label
and "width" in label
and "height" in label
and "rotation" in label
):
return None
org_width, org_height = label["original_width"], label["original_height"]
x = label["x"] / 100 * org_width
y = label["y"] / 100 * org_height
w = label["width"] / 100 * org_width
h = label["height"] / 100 * org_height
rotation = math.radians(label.get("rotation", 0))
cos, sin = math.cos(rotation), math.sin(rotation)
coords = [
(x, y),
(x + w * cos, y + w * sin),
(x + w * cos - h * sin, y + w * sin + h * cos),
(x - h * sin, y + h * cos),
]
# Normalize coordinates
if normalize:
return [(coord[0] / org_width, coord[1] / org_height) for coord in coords]
else:
return coords