v9.1.1
Menu

Procrustes Analysis

mediumFREELeast Squares AlignmentAlignmentCV: Image Alignment and Stitching

Implement Procrustes analysis for optimal rigid alignment of point sets. This technique is crucial in computer vision and image processing for aligning two sets of points that differ by a rigid transformation, which includes rotation and translation.

The goal is to find the optimal rotation matrix RR and translation vector tt that minimizes the sum of squared errors between the two point sets. Given two point sets P={pi}P = \{p_i\} and Q={qi}Q = \{q_i\}, the objective is to minimize ∑i∥R⋅pi+t−qi∥2\sum_i \|R \cdot p_i + t - q_i\|^2.

  1. Center both point sets by subtracting their respective centroids.
  2. Compute the covariance matrix of the centered point sets.
  3. Use Singular Value Decomposition (SVD) to find the optimal rotation.
∑i∥R⋅pi+t−qi∥2\sum_i \|R \cdot p_i + t - q_i\|^2

RR must be a proper rotation (det⁡R=+1\det R = +1), never a reflection.

Return a dict with:

  • 'R': the 2×22 \times 2 rotation matrix as a nested list, rounded to 4 decimal places
  • 't': the translation vector as a list, rounded to 4 decimal places
  • 'error': the root-mean-square distance between the aligned source and the target, 1N∑i∥R⋅pi+t−qi∥2\sqrt{\frac{1}{N}\sum_i \|R \cdot p_i + t - q_i\|^2}, rounded to 4 decimal places

This technique is widely used in image registration and object recognition.

Example

Input
source = [[0,0], [1,0], [0,1]]
target = [[1,1], [2,1], [1,2]]  # Translated by (1,1)
Output
R=identity, t=[1,1], error≈0
Reasoning
  • Inputs: source holds N=3N = 3 points pip_i in 2D, target holds the matching qiq_i. Point ii in one set corresponds to point ii in the other, so each target point here is its source point shifted by (1,1)(1, 1).
  • Centroids: pˉ=(0+1+03,0+0+13)=(13,13)\bar p = \left(\tfrac{0+1+0}{3}, \tfrac{0+0+1}{3}\right) = \left(\tfrac13, \tfrac13\right) and qˉ=(43,43)\bar q = \left(\tfrac43, \tfrac43\right).
  • Centered points: both sets become (−13,−13),(23,−13),(−13,23)\left(-\tfrac13,-\tfrac13\right), \left(\tfrac23,-\tfrac13\right), \left(-\tfrac13,\tfrac23\right). The shapes are identical once the shift is removed.
  • Covariance: H=∑ip~iq~i⊤=[23−13−1323]H = \sum_i \tilde p_i \tilde q_i^{\top} = \begin{bmatrix} \tfrac23 & -\tfrac13 \\ -\tfrac13 & \tfrac23 \end{bmatrix}. It is symmetric with positive eigenvalues 11 and 13\tfrac13, so no rotation lines the centered sets up better than none: R=IR = I.
  • Translation: t=qˉ−Rpˉ=(43−13,43−13)=(1,1)t = \bar q - R\bar p = \left(\tfrac43 - \tfrac13, \tfrac43 - \tfrac13\right) = (1, 1).
  • Error: every Rpi+tR p_i + t lands exactly on qiq_i, so the RMS distance is 00. Notice tt is not just qˉ−pˉ\bar q - \bar p in general: when R≠IR \neq I the source centroid must be rotated first.

Constraints

  • source: Source points (N, 2)
  • target: Target points (N, 2)
  • Return: Dict with 'R' (2×2 rotation), 't' (translation), 'error'

More from CV: Image Alignment and Stitching

solution.py

Test results

Sign in to run your code against the test cases.