简体   繁体   中英

How to convert Bounding Box coordinates to COCO format?

I need to convert the coordinates. I have this format: Horizontal and Vertical coordinates of the top left and lower right of the element ((x1, y1) and (x2, y2)). And I need x_center y_center width height in this format. How can I do it?

The center and the size are simply

x_center = 0.5 * (x1 + x2)
y_center = 0.5 * (y1 + y2)
width = np.abs(x2 - x1)
height = np.abs(y2 - y1)

Note that by using np.abs when computing the width and height we do need to assume anything on the "order" of the first and second corners.

If you further want to normalize the center and size by the image size (img_w, img_h) :

n_x_center = x_center / img_w
n_y_center = y_center / img_h
n_width = width / img_w
n_height = height / img_h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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