繁体   English   中英

如何将cv2.rectangle边界框转换为YoloV4标注格式(相对x,y,w,h)?

[英]How to convert cv2.rectangle bounding box to YoloV4 annotation format (relative x,y,w,h)?

我训练了一个 Yolo4.network,它给我的边界框如下:

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 之间的浮动值。

让我们假设我的价值观是:

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格式? 这有点令人困惑。 我使用了这个答案中的许多代码似乎不起作用。

另外,我尝试使用此代码,但我不知道那是否正确。 即使那是对的,我也不知道如何获得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_

猜猜我已经接近解决xy不是绝对的,而是AlexyAB 在这个答案中描述的矩形框的中心。 所以我跟进了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

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

有一种更直接的方法可以用pybboxes做这些事情。 安装,

pip install pybboxes

在你的情况下,

import pybboxes as pbx

voc_bbox = (489, 126, 161, 216)
W, H = 443, 1265  # WxH of the image
pbx.convert_bbox(voc_bbox, from_type="coco", to_type="yolo", image_width=W, image_height=H)
>>> (1.2855530474040633, 0.18498023715415018, 0.36343115124153497, 0.1707509881422925)

请注意,转换为 YOLO 格式需要图像宽度和高度进行缩放。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM