【问题标题】:How to convert 2D bounding box pixel coordinates (x, y, w, h) into relative coordinates (Yolo format)?如何将 2D 边界框像素坐标(x、y、w、h)转换为相对坐标(Yolo 格式)?
【发布时间】:2020-11-01 16:40:32
【问题描述】:

喂!我正在通过在线平台注释图像数据,该平台生成如下输出坐标:bbox":{"top":634,"left":523,"height":103,"width":145} 但是,我想用这个注解来训练 Yolo。所以,我必须将它转换为 yolo 格式,如下所示:4 0.838021 0.605556 0.177083 0.237037

在这方面,我需要有关如何转换它的帮助。

【问题讨论】:

    标签: python computer-vision conv-neural-network yolo


    【解决方案1】:

    这里, 对于你需要传递的尺寸 (w,h) 和你需要传递的盒子 (x,x+w, y, y+h) https://github.com/ivder/LabelMeYoloConverter/blob/master/convert.py

    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)
    

    或者,您可以在下面使用

    def convert(x,y,w,h):
     dw = 1.0/w
     dh = 1.0/h
     x = (2*x+w)/2.0
     y = (2*y+w)/2.0
     x = x*dw
     y = y*dh
     w = w*dw
     h = h*dh
     return (x,y,w,h)
    

    每个网格单元预测 B 个边界框以及 C 类概率。边界框预测有 5 个分量:(x, y, w, h, confidence)。 (x, y) 坐标表示盒子的中心,相对于网格单元的位置(请记住,如果盒子的中心不在网格单元内,则该单元不对此负责)。这些坐标被归一化为介于 0 和 1 之间。相对于图像大小, (w, h) 框尺寸也被归一化为 [0, 1]。我们来看一个例子:

    What does the coordinate output of yolo algorithm represent?

    【讨论】:

    • 感谢亲爱的@rcvaram 的回复。请需要参数说明,即 w,h。
    • 是图片的宽度和高度,还是边界框?
    • 是的,这个 x 和 y 是您的边界框的中心,并且 w,h 是边界框的宽度和高度
    • 我明白了。但是,我对以下几点感到困惑。让我解释。我有一张 1920x1080 的图像,并且我绘制了一个包含以下信息的边界框。 x,y 坐标为 1167, 537px(边界框左上角坐标) 高度:224px 宽度:320px 有了这些信息,我想对其进行归一化。
    【解决方案2】:

    将bbox字典转换为具有相对坐标的列表

    如果你想用top、left、widht、height键转换python字典 以 [x1, y1, x2, y2] 格式放入列表中

    其中x1、y1是边界框top left corner的相对坐标,x2、y2是边界框bottom right corner的相对坐标可以使用如下功能:

    def bbox_dict_to_list(bbox_dict, image_size):
      h = bbox_dict.get('height')
      l = bbox_dict.get('left')
      t = bbox_dict.get('top')
      w = bbox_dict.get('width')
    
      img_w, img_h = image_size
    
      x1 = l/img_w
      y1 = t/img_h
      x2 = (l+w)/img_w
      y2 = (t+h)/img_h
      return [x1, y1, x2, y2]
    

    您必须将 bbox 字典作为参数传递,并将图像大小作为元组传递 -> (image_width, image_height)

    例子

    bbox = {"top":634,"left":523,"height":103,"width":145} 
    bbox_dict_to_list(bbox, (1280, 720))
    >> [0.40859375, 0.8805555555, 0.521875, 1.02361111111]
    

    您可以根据需要更改退货单

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 2022-12-28
      • 2021-09-03
      • 2021-01-13
      • 2020-04-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      相关资源
      最近更新 更多