在图像上标记目标的矩形(bounding box, bbox)。常见的标注格式为Pascal VOC、COCO、YOLO
Pascal VOC
bbox:[x_min, y_min, x_max, y_max]
格式:左上右下
COCO
bbox:[x_min, ymin, width, height]
格式:左上宽高
YOLO
bbox [x_center, y_center, width, height]
并进行数据规范化(normalized)
格式:中心坐标,宽高
Pasic VOC 转 YOLO
def convert_box(size, box):
# Convert VOC box to YOLO xywh box
dw = 1. / size[0]
dh = 1. / size[1]
return ((box[0] + box[1]) / 2.0 * dw, (box[2] + box[3]) / 2.0 * dh , (box[1] - box[0]) * dw, (box[3] - box[2]) * * dh)
COCO 转 YOLO
def convert_box(size, box):
# Convert COCO box to YOLO xywh box
dw = 1. / size[0]
dh = 1. / size[1]
return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh
YOLO Decode
def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
# 将yolo格式的box直接读取
# Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x
y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y
y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x
y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y
return y