v9.1.1
Menu

ICP Point Cloud Alignment

hardPRO3D ScanningCVCV: 3D Reconstruction

Implement Iterative Closest Point (ICP) for 3D scan alignment, a 3D registration technique used to find the best alignment between two point clouds. ICP is crucial in 3D reconstruction as it enables the combination of multiple scans into a single, cohesive model.

The ICP process involves iteratively finding the closest points between the source and target point clouds, estimating the rigid transformation (RR, tt) that best aligns the two clouds, and applying this transformation to the source cloud. This process is repeated until convergence, at which point the source and target clouds are optimally aligned.

To achieve this, the following steps are taken:

  1. Find closest points between source and target point clouds
  2. Estimate rigid transformation (RR, tt) using the Procrustes method
  3. Apply transformation to source point cloud
  4. Repeat until convergence

Implement icp(source, target, max_iter), running at most max_iter iterations, and return a tuple (aligned, R, t): the aligned source cloud of shape (n,3)(n, 3), plus the 3×33 \times 3 rotation RR and length-3 translation tt estimated in the final iteration.

R=arg⁡min⁡R∈SO(3)∑i=1n∣∣R⋅xi+t−yi∣∣2R = \arg \min_{R \in SO(3)} \sum_{i=1}^{n} ||R \cdot \mathbf{x}_i + \mathbf{t} - \mathbf{y}_i||^2

This technique is widely used in computer vision and robotics for tasks such as 3D mapping and object recognition.

Example

Input
Two point clouds
Output
Aligned source cloud
Reasoning
  • Inputs: source and target are n×3n \times 3 arrays of points, and max_iter caps the number of match → align rounds. Example: source is the triangle (0,0,0),(1,0,0),(0,1,0)(0,0,0), (1,0,0), (0,1,0) and target is the same triangle shifted by (0.1,0.2,0)(0.1, 0.2, 0).
  • Matching: each source point's closest target point is its own shifted copy (distance 0.12+0.22≈0.224\sqrt{0.1^2 + 0.2^2} \approx 0.224, closer than any other target point).
  • Centroids: the source centroid is xˉ=(13,13,0)\bar{x} = (\tfrac13, \tfrac13, 0) and the matched centroid is yˉ=(0.4333,0.5333,0)\bar{y} = (0.4333, 0.5333, 0). After subtracting them, both triangles are identical, so the best rotation is R=IR = I and t=yˉ−Rxˉ=(0.1,0.2,0)t = \bar{y} - R\bar{x} = (0.1, 0.2, 0).
  • Output: applying Rxi+tR\mathbf{x}_i + t gives the Aligned source cloud (0.1,0.2,0),(1.1,0.2,0),(0.1,1.2,0)(0.1, 0.2, 0), (1.1, 0.2, 0), (0.1, 1.2, 0) (exactly the target) after one round. It is returned with the 3×33 \times 3 RR and length-3 tt of the last round.
  • Notice: every later round finds zero residual, so the last round's step is R=IR = I, t=(0,0,0)t = (0, 0, 0), not the total shift.

Constraints

  • Input point clouds: 3D numpy arrays of shape (n, 3) where n is the number of points in the cloud
  • Valid ranges: Point cloud coordinates are floating-point numbers in the range [-100, 100]
  • Output format: Return the aligned source point cloud as a 3D numpy array of shape (n, 3) with floating-point precision
  • Special conditions: The input point clouds are not empty and contain at least 3 points, and the target point cloud is not identical to the source point cloud
  • Convergence criteria: The iteration stops when the difference between the current and previous transformations is less than a threshold of 1e-6 or when a maximum of 100 iterations is reached

Learn this in

More from CV: 3D Reconstruction

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.