将包含旋转框信息的数据集格式从中心坐标、宽度、高度和旋转角度(cx, cy, w, h, angle)转换为四个顶点坐标(x1, y1, x2, y2, x3, y3, x4, y4)需要进行一些几何变换:
def convert_rotated_box_to_quad(cx, cy, w, h, angle):
# 旋转角度转换为弧度
angle_rad = np.radians(angle)
# 计算旋转矩阵
rotation_matrix = np.array([[np.cos(angle_rad), -np.sin(angle_rad)],
[np.sin(angle_rad), np.cos(angle_rad)]])
# 计算旋转后的四个角点相对于中心的坐标
box_corners = np.array([[-w / 2, -h / 2],
[w / 2, -h / 2],
[w / 2, h / 2],
[-w / 2, h / 2]])
rotated_corners = np.dot(box_corners, rotation_matrix.T)
# 平移中心点
rotated_corners[:, 0] += cx
rotated_corners[:, 1] += cy
# 将坐标转换为四个顶点的坐标
x1, y1 = rotated_corners[0]
x2, y2 = rotated_corners[1]
x3, y3 = rotated_corners[2]
x4, y4 = rotated_corners[3]
return x1, y1, x2, y2, x3, y3, x4, y4