【问题标题】:How to convert cv2.rectangle bounding box to YoloV4 annotation format (relative x,y,w,h)?如何将 cv2.rectangle 边界框转换为 YoloV4 注释格式(相对 x、y、w、h)?
【发布时间】:2021-04-15 14:53:13
【问题描述】:

我已经训练了一个 Yolo4 网络,它给我的边界框如下:

img_array = cv2.cvtColor(cv2.imread('image.png'), cv2.COLOR_BGR2RGB)
classes, scores, bboxes = model.detect(img_array, CONFIDENCE_THRESHOLD, NMS_THRESHOLD)

box = bboxes[0]

(x, y) = (box[0], box[1])
(w, h) = (box[2], box[3])

当我使用cv2.rectangle 保存图像时:

cv2.rectangle(img_array, (x, y), (x + w, y + h), (127,0,75), 1)

cv2.imwrite('image.png',img_array)

IT 给了我一个很好的边界框。我想使用这个box 和图像数组的形状来创建一个文本文件,该文件采用Yolov4 格式作为x,y,w,h 浮动值介于0 和1 相对 图像大小之间。 p>

假设我的价值观是:

img_array.shape -> (443, 1265, 3)
box -> array([489, 126, 161, 216], dtype=int32)

所以它给了我

(x, y) = (box[0], box[1]) -> (489, 126)
(w, h) = (box[2], box[3]) -> (161, 216)

我在文本文件中使用LabelImg 创建的边界框也为

0.453125 0.538462 0.132212 0.509615 # 0 is the class

如何使用这些坐标获取Yolov4 格式?这有点令人困惑。我使用了很多来自this answer 的代码似乎不起作用。

另外,我尝试使用此代码,但我不知道这是否正确。即使是这样,我也不知道如何获得x_, y_

def yolov4_format(img_shape,box):
    x_img, y_img, c = img_shape
    (x, y) = (box[0], box[1])
    (w, h) = (box[2], box[3])
    
    x_, y_ = None # logic for these?
    w_ = w/x_img
    h_ = h/y_img
    return x_,y_, w_, h_

【问题讨论】:

    标签: python numpy opencv computer-vision yolo


    【解决方案1】:

    猜我已经接近解决 xyNOT 绝对但矩形框的中心为 described by AlexyAB in this answer。于是我跟进code for LabelImg,找到了一段代码,修改为我的用例。

    def bnd_box_to_yolo_line(box,img_size):
            (x_min, y_min) = (box[0], box[1])
            (w, h) = (box[2], box[3])
            x_max = x+w
            y_max = y+h
            
            x_center = float((x_min + x_max)) / 2 / img_size[1]
            y_center = float((y_min + y_max)) / 2 / img_size[0]
    
            w = float((x_max - x_min)) / img_size[1]
            h = float((y_max - y_min)) / img_size[0]
    
            return x_center, y_center, w, h
    

    您只需要边界框和图像形状

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-01
      • 2019-09-30
      • 2022-12-28
      • 2023-01-27
      • 2021-07-18
      • 2018-10-11
      • 2021-06-15
      • 1970-01-01
      相关资源
      最近更新 更多