v9.1.1
Menu

Estimate Translation

easyPROLeast Squares AlignmentAlignmentCV: Image Alignment and Stitching

Implement a function to estimate the optimal translation that aligns two point sets with known correspondences. This problem is rooted in Least Squares Alignment, a fundamental concept in Image Alignment and Stitching, where the goal is to find the best transformation that minimizes the distance between two sets of points.

The translation vector that achieves this alignment can be found by considering the centroids of the source and target point sets, which are the average positions of all points in each set. The optimal translation is the vector that moves the centroid of the source to the centroid of the target.

Here are the steps to estimate the translation:

  1. Compute the centroid of the source point set.
  2. Compute the centroid of the target point set.
  3. Calculate the translation vector as the difference between the target and source centroids.
t=qˉ−pˉ\mathbf{t} = \bar{\mathbf{q}} - \bar{\mathbf{p}}

This technique is widely used in computer vision applications, such as image registration and object tracking.

Example

Input
source = [(0, 0), (1, 0)]
target = [(5, 5), (6, 5)]
Output
(5.0, 5.0)
Reasoning
  • Inputs: source holds n=2n = 2 points p1=(0,0)\mathbf{p}_1 = (0, 0), p2=(1,0)\mathbf{p}_2 = (1, 0), and target holds their partners q1=(5,5)\mathbf{q}_1 = (5, 5), q2=(6,5)\mathbf{q}_2 = (6, 5). Point ii of one list corresponds to point ii of the other.
  • Source centroid: pˉ=(0+12,0+02)=(0.5,0)\bar{\mathbf{p}} = \left(\frac{0 + 1}{2}, \frac{0 + 0}{2}\right) = (0.5, 0).
  • Target centroid: qˉ=(5+62,5+52)=(5.5,5)\bar{\mathbf{q}} = \left(\frac{5 + 6}{2}, \frac{5 + 5}{2}\right) = (5.5, 5).
  • Translation: t=qˉ−pˉ=(5.5−0.5, 5−0)=(5,5)\mathbf{t} = \bar{\mathbf{q}} - \bar{\mathbf{p}} = (5.5 - 0.5,\ 5 - 0) = (5, 5), rounded to 4 decimal places, giving (5.0, 5.0) as a pair (tx,ty)(t_x, t_y).
  • Check: adding t\mathbf{t} to each source point gives (5,5)(5, 5) and (6,5)(6, 5), exactly the targets, so the squared alignment error ∑i∥pi+t−qi∥2\sum_i \lVert \mathbf{p}_i + \mathbf{t} - \mathbf{q}_i \rVert^2 is 00.
  • Notice: with noisy points no single shift fits every pair, yet the centroid difference still minimises that sum of squared errors. Each pair "votes" for qi−pi\mathbf{q}_i - \mathbf{p}_i and the result is the average vote.

Constraints

  • source and target are lists of corresponding (x, y) points
  • Return translation as (tx, ty) tuple
  • Round to 4 decimal places

More from CV: Image Alignment and Stitching

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.