如何将形式 [xmin ymin xmax ymax] 转换为图像中标准化的 [xy 宽度高度]?

gli*_*ima 2 python object-detection microsoft-custom-vision

我正在使用 Microsoft 的CustomVision.ai构建自定义视觉应用程序。

我正在使用本教程

在对象检测项目中标记图像时,需要使用标准化坐标指定每个标记对象的区域。

我有一个 XML 文件,其中包含有关图像的注释,例如名为sample_1.jpg

<annotation>
        <filename>sample_1.jpg</filename>
    <size>
        <width>410</width>
        <height>400</height>
        <depth>3</depth>
    </size>
    <object>
        <bndbox>
            <xmin>159</xmin>
            <ymin>15</ymin>
            <xmax>396</xmax>
            <ymax>302</ymax>
        </bndbox>
    </object>
</annotation>
Run Code Online (Sandbox Code Playgroud)

我必须根据提供的教程将边界框坐标从 xmin,xmax,ymin,ymax 转换为标准化的 x,y,w,h 坐标。

谁能给我一个转换函数?

小智 8

假设 x/ymin 和 x/ymax 分别是你的边界角,左上角和右下角。然后:

x = xmin
y = ymin
w = xmax - xmin
h = ymax - ymin
Run Code Online (Sandbox Code Playgroud)

然后您需要对这些进行归一化,这意味着将它们作为整个图像的比例,因此简单地将每个值除以上述值的各自大小:

x = xmin / width
y = ymin / height
w = (xmax - xmin) / width
h = (ymax - ymin) / height
Run Code Online (Sandbox Code Playgroud)

这假设原点为左上角,如果不是这种情况,则必须应用移位因子。


nul*_*ull 5

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

pip install pybboxes
Run Code Online (Sandbox Code Playgroud)

就你而言,

import pybboxes as pbx

voc_bbox = (159, 15, 396, 302)
W, H = 410, 400  # WxH of the image
pbx.convert_bbox(voc_bbox, from_type="voc", to_type="coco")
>>> (159, 15, 237, 287)
Run Code Online (Sandbox Code Playgroud)

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