Search code examples
pythoncomputer-visionpytorchobject-detectionyolo

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?


Solution

  • 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