v9.1.1
Menu

Compute Centroid

easyFREELeast Squares AlignmentAlignmentCV: Image Alignment and Stitching

You are given a set of 2D points and need to compute their centroid (center of mass).

The centroid is the average position of all points: xˉ=1n∑i=1nxi,yˉ=1n∑i=1nyi\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i, \quad \bar{y} = \frac{1}{n}\sum_{i=1}^{n} y_i

Centroids are fundamental in alignment algorithms because:

  1. They're used to normalize point sets before computing transformations
  2. They help separate translation from rotation/scaling
  3. They minimize the sum of squared distances to all points

In Procrustes analysis and ICP (Iterative Closest Point), centering point sets at the origin simplifies the computation of optimal rotation.

Example

Input
points = [(0, 0), (2, 0), (2, 2), (0, 2)]
Output
(1.0, 1.0)
Reasoning
  • Input: points holds n=4n = 4 pairs (xi,yi)(x_i, y_i): the corners (0,0)(0,0), (2,0)(2,0), (2,2)(2,2), (0,2)(0,2) of a square with side length 2.
  • x coordinate: the four xx values are 0,2,2,00, 2, 2, 0. Their total is 4, so xˉ=44=1\bar{x} = \frac{4}{4} = 1.
  • y coordinate: the four yy values are 0,0,2,20, 0, 2, 2, which also total 4, so yˉ=44=1\bar{y} = \frac{4}{4} = 1.
  • Output: the pair (xˉ,yˉ)(\bar{x}, \bar{y}) as a tuple of floats, each rounded to 4 decimal places, giving (1.0, 1.0). The division makes both values floats even though every input is an integer.
  • Notice: the centroid is not one of the input points. It sits at the centre of the square, at distance 12+12=2\sqrt{1^2 + 1^2} = \sqrt{2} from every corner, so the squared distances total 4×2=84 \times 2 = 8, the smallest total any point in the plane can achieve. With a single point, the centroid is that point itself.

Constraints

  • points is a list of (x, y) tuples
  • Return centroid as (x, y) tuple
  • Round each coordinate to 4 decimal places

More from CV: Image Alignment and Stitching

solution.py

Test results

Sign in to run your code against the test cases.