Python 图像中矩形四角二维坐标和归一化一维坐标相互转换
·
Python 将图像中矩形四角二维坐标转化为归一化一维坐标
计算原理:根据矩形四角的二维坐标得出矩形中心的坐标,再计算出矩形的宽和高,由此可将四个二维坐标转化为四个一维坐标 。归一化即x轴的值除以图像宽度,y轴的值除以图像高度。
[中心坐标x, 中心坐标y, 矩形宽, 矩形高]
def normalize_bbox_coordinates(bbox_coords, image_width=500, image_height=500):
"""
将矩形框坐标归一化到[0,1]范围
:param bbox_coords: 矩形框四个坐标点 [(x1,y1), (x2,y2), (x3,y3), (x4,y4)]
:param image_width: 图片宽度,默认500
:param image_height: 图片高度,默认500
:return list: 归一化后的坐标 [x_center, y_center, width, height]
"""
# 提取所有x坐标和y坐标
x_coords = [point[0] for point in bbox_coords]
y_coords = [point[1] for point in bbox_coords]
# 计算边界框的最小和最大坐标
x_min = min(x_coords)
x_max = max(x_coords)
y_min = min(y_coords)
y_max = max(y_coords)
# 计算中心点坐标和宽高
width = x_max - x_min
height = y_max - y_min
x_center = x_min + width / 2
y_center = y_min + height / 2
# 归一化到[0,1]范围
x_center_norm = x_center / image_width
y_center_norm = y_center / image_height
width_norm = width / image_width
height_norm = height / image_height
return [x_center_norm, y_center_norm, width_norm, height_norm]
bbox_coords = [(125, 125), (125, 375), (375, 375), (375, 125)]
normalized_coords = normalize_bbox_coordinates(bbox_coords)
print(f"原始坐标: {bbox_coords}")
print(f"归一化坐标: {normalized_coords}")
print(f"验证结果: {[round(coord, 1) for coord in normalized_coords]}")
原始坐标: [(125, 125), (125, 375), (375, 375), (375, 125)]
归一化坐标: [0.5, 0.5, 0.5, 0.5]
验证结果: [0.5, 0.5, 0.5, 0.5]
Python 将图像中归一化一维坐标转化为矩形四角二维坐标
def denormalize_coordinates(normalized_coords, image_width=500, image_height=500):
"""
将归一化坐标复原到原始图像尺寸
:param normalized_coords: 归一化坐标 [x_center_norm, y_center_norm, width_norm, height_norm]
:param image_width: 图片宽度,默认500
:param image_height: 图片高度,默认500
:return tuple: (center_x, center_y, width, height) 原始坐标和尺寸
"""
if len(normalized_coords) != 4:
raise ValueError("归一化坐标必须是4个值: [x_center, y_center, width, height]")
x_center_norm, y_center_norm, width_norm, height_norm = normalized_coords
# 复原中心坐标
center_x = x_center_norm * image_width
center_y = y_center_norm * image_height
# 复原宽度和高度
width = width_norm * image_width
height = height_norm * image_height
return center_x, center_y, width, height
def denormalize_to_corners(normalized_coords, image_width=500, image_height=500):
"""
将归一化坐标复原为四个角点坐标
:param normalized_coords: 归一化坐标 [x_center_norm, y_center_norm, width_norm, height_norm]
:param image_width: 图片宽度,默认500
:param image_height: 图片高度,默认500
:return list: 四个角点坐标 [(x1,y1), (x2,y2), (x3,y3), (x4,y4)]
"""
center_x, center_y, width, height = denormalize_coordinates(normalized_coords, image_width, image_height)
# 计算角点坐标
x_min = center_x - width / 2
x_max = center_x + width / 2
y_min = center_y - height / 2
y_max = center_y + height / 2
# 返回四个角点(顺时针顺序)
corners = [
(x_min, y_min), # 左上
(x_min, y_max), # 左下
(x_max, y_max), # 右下
(x_max, y_min) # 右上
]
return corners
# 归一化坐标
normalized_coords = [0.5, 0.5, 0.5, 0.5]
# 复原中心坐标和尺寸
center_x, center_y, width, height = denormalize_coordinates(normalized_coords)
print(f"归一化坐标: {normalized_coords}")
print(f"复原中心坐标: ({center_x}, {center_y}), 尺寸: {width}x{height}")
# 复原为四个角点
corners = denormalize_to_corners(normalized_coords)
print(f"复原角点坐标: {corners}")
# 验证复原结果是否与原始坐标一致
original_corners = [(125, 125), (125, 375), (375, 375), (375, 125)]
print(f"原始角点坐标: {original_corners}")
print(f"复原是否正确: {corners == original_corners}")
归一化坐标: [0.5, 0.5, 0.5, 0.5]
复原中心坐标: (250.0, 250.0), 尺寸: 250.0x250.0
复原角点坐标: [(125.0, 125.0), (125.0, 375.0), (375.0, 375.0), (375.0, 125.0)]
原始角点坐标: [(125, 125), (125, 375), (375, 375), (375, 125)]
复原是否正确: True
更多推荐


所有评论(0)