Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

20. Bounding box representation

easy
GeneralGeneral
senior

Explain and implement a simple bounding box conversion utility used in computer vision, where different models and datasets store boxes in different formats.

A common conversion is from corner format ((x_{\min}, y_{\min}, x_{\max}, y_{\max})) to center format ((c_x, c_y, w, h)), defined as:

cx=xmin+xmax2,cy=ymin+ymax2,w=xmaxxmin,h=ymaxyminc_x=\frac{x_{\min}+x_{\max}}{2},\quad c_y=\frac{y_{\min}+y_{\max}}{2},\quad w=x_{\max}-x_{\min},\quad h=y_{\max}-y_{\min}

Requirements

Implement the function

python

Rules:

  • Use vectorized NumPy operations.
  • Avoid Python loops.
  • boxes will be an array of shape (N, 4).
  • Return an array of shape (N, 4).

Example

python

Output:

python
Input Signature
ArgumentType
boxesnp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Return NumPy array, not Python list

  • Use NumPy vectorized ops; no torchvision utils

  • Compute using floating-point dtype

Hint 1

Start by converting boxes into a NumPy array of dtype=float so arithmetic stays floating-point and can be vectorized.

Hint 2

Use column slicing: x_min = arr[:,0], y_min = arr[:,1], x_max = arr[:,2], y_max = arr[:,3] and apply the given formulas elementwise.

Hint 3

Reassemble with np.stack([c_x, c_y, w, h], axis=1).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
bounding-boxes
numpy-vectorization
computer-vision
coordinate-transforms
24 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit