首页 > 解决方案 > 如何将边界框坐标转换为 Yolo 坐标?

问题描述

我正在尝试将边界框坐标转换为 Yolo 坐标。边界框坐标不是典型格式。它们看起来像这样:

1,-1,855,884,94,195,1,-1,-1,-1
1,-1,1269,830,103,202,0,-1,-1,-1
1,-1,1023,909,86,170,0,-1,-1,-1
1,-1,879,681,76,191,0,-1,-1,-1

如何使用 1s、-1s 和 0s 将这些坐标转换为 Yolo 格式?

我尝试使用此代码将它们转换为 Yolo:

def convert(filename_str, coords):
    os.chdir("..")
    image = cv2.imread(filename_str + ".jpg")
    coords[2] -= coords[0]
    coords[3] -= coords[1]
    x_diff = int(coords[2]/2)
    y_diff = int(coords[3]/2)
    coords[0] = coords[0]+x_diff
    coords[1] = coords[1]+y_diff
    coords[0] /= int(image.shape[1])
    coords[1] /= int(image.shape[0])
    coords[2] /= int(image.shape[1])
    coords[3] /= int(image.shape[0])
    os.chdir("Label")
    return coords

我得到了这种格式的负 Yolo 坐标:

0 0.2871825876662636 0.5 -0.46009673518742444 -0.637962962962963
0 0.4147521160822249 0.4777777777777778 -0.7049576783555018 -0.5814814814814815
0 0.3355501813784764 0.5 -0.5665054413542926 -0.6842592592592592

提前致谢

标签: coordinatesyoloboxbounding

解决方案


尝试这个:

def convert(size, box):
    dw = 1./size[0]
    dh = 1./size[1]
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

im=Image.open(img_path)
w= int(im.size[0])
h= int(im.size[1])


print(xmin, xmax, ymin, ymax) #define your x,y coordinates
b = (xmin, xmax, ymin, ymax)
bb = convert((w,h), b)

推荐阅读