[英]How to convert cv2.rectangle bounding box to YoloV4 annotation format (relative x,y,w,h)?
[英]How to convert 2D bounding box pixel coordinates (x, y, w, h) into relative coordinates (Yolo format)?
嗨! 我正在通過在線平台注釋圖像數據,該平台生成這樣的輸出坐標: bbox":{"top":634,"left":523,"height":103,"width":145}但是,我想使用此注釋來訓練 Yolo。所以,我必須將其轉換為 yolo 格式,如下所示: 4 0.838021 0.605556 0.177083 0.237037
在這方面,我需要有關如何轉換它的幫助。
在這里,對於需要傳遞 (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、置信度)。 (x, y) 坐標表示框的中心,相對於網格單元的位置(請記住,如果框的中心不在網格單元內,則該單元不對其負責)。 這些坐標被歸一化為介於 0 和 1 之間。 (w, h) 框尺寸也被歸一化為 [0, 1],相對於圖像大小。 讓我們看一個例子:
如果要將帶有鍵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]
您可以更改退貨單以滿足您的需求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.