How can I convert form [xmin ymin xmax ymax] to [x y width height] normalized in image?
Asked Answered
S

3

4

I am building a custom vision application with Microsoft's CustomVision.ai.

I am using this tutorial.

When you tag images in object detection projects, you need to specify the region of each tagged object using normalized coordinates.

I have an XML file containing the annotations about the image, e.g. named 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>

I have to convert the bounding box coordinates from xmin,xmax,ymin,ymax to x,y,w,h coordinates normalized according to the provided tutorial.

Can anyone provide me a conversion function?

Selfrising answered 11/7, 2019 at 8:43 Comment(1)
what does x and y mean in this case?Dink
L
14

Assuming x/ymin and x/ymax are your bounding corners, top left and bottom right respectively. Then:

x = xmin
y = ymin
w = xmax - xmin
h = ymax - ymin

You then need to normalize these, which means give them as a proportion of the whole image, so simple divide each value by its respective size from the values above:

x = xmin / width
y = ymin / height
w = (xmax - xmin) / width
h = (ymax - ymin) / height

This assumes a top-left origin, you will have to apply a shift factor if this is not the case.

Lily answered 11/7, 2019 at 9:0 Comment(0)
B
5

There is a more straight-forward way to do those stuff with pybboxes. Install with,

pip install pybboxes

In your case,

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)

Note that, converting to YOLO format requires the image width and height for scaling.

Blackguard answered 1/5, 2022 at 9:15 Comment(1)
This approach should be in every bounding box related answer.Mayorga
Y
2

Here's a function that converts the values and normalizes them for the image size:

def convert(xmin, ymin, xmax, ymax, img_w, img_h):
    dw = 1./(img_w)
    dh = 1./(img_h)
    x = (xmin + xmax)/2.0 - 1
    y = (ymin + ymax)/2.0 - 1
    w = xmax - xmin
    h = ymax - ymin
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

And for your example above:

my_xmin = 159
my_ymin = 15
my_xmax = 396
my_ymax = 302
my_img_w = 410
my_img_h = 400
convert(my_xmin, my_ymin, my_xmax, my_ymax, my_img_w, my_img_h)
Yockey answered 1/11, 2021 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.