v9.1.1
Menu

ICP Nearest Neighbor

mediumPRO3D ScanningScanningCV: 3D Reconstruction

Implement a function to find the closest point in a target point cloud for each source point, a crucial step in the Iterative Closest Point (ICP) algorithm. This process is essential in 3D scanning and reconstruction to align two point clouds by establishing correspondences between them.

The ICP algorithm relies on the concept of Euclidean distance to measure the proximity between points in 3D space, calculated as d(p,q)=(px−qx)2+(py−qy)2+(pz−qz)2d(p, q) = \sqrt{(p_x - q_x)^2 + (p_y - q_y)^2 + (p_z - q_z)^2}. To find the closest point, one must iterate through all target points and determine which point yields the minimum distance for each source point.

Here are the general steps involved:

  1. Iterate over each source point
  2. For each source point, calculate the distance to all target points
  3. Identify the target point with the minimum distance for each source point
d(p,q)=(px−qx)2+(py−qy)2+(pz−qz)2d(p, q) = \sqrt{(p_x - q_x)^2 + (p_y - q_y)^2 + (p_z - q_z)^2}

This technique is widely used in computer vision and robotics for 3D reconstruction and object recognition.

Example

Input
find_closest([[0, 0, 0], [1, 0, 0]], [[0.1, 0, 0], [0.9, 0, 0], [5, 5, 5]])
Output
[0, 1]
Reasoning
  • Inputs: source_points holds two 3D points, s0=(0,0,0)s_0 = (0,0,0) and s1=(1,0,0)s_1 = (1,0,0); target_points holds three, t0=(0.1,0,0)t_0 = (0.1,0,0), t1=(0.9,0,0)t_1 = (0.9,0,0) and t2=(5,5,5)t_2 = (5,5,5). Each target is identified by its position in the list.
  • Source s0s_0: d(s0,t0)=0.1d(s_0,t_0) = 0.1, d(s0,t1)=0.9d(s_0,t_1) = 0.9, and d(s0,t2)=52+52+52=75≈8.660d(s_0,t_2) = \sqrt{5^2+5^2+5^2} = \sqrt{75} \approx 8.660. The smallest is t0t_0, so its match is index 00.
  • Source s1s_1: d(s1,t0)=0.9d(s_1,t_0) = 0.9, d(s1,t1)=0.1d(s_1,t_1) = 0.1, and d(s1,t2)=42+52+52=66≈8.124d(s_1,t_2) = \sqrt{4^2+5^2+5^2} = \sqrt{66} \approx 8.124. The smallest is t1t_1, so its match is index 11.
  • Output: [0, 1], one target index per source point, in source order. It holds indices, not coordinates or distances.
  • Notice that comparing squared distances (0.010.01 vs 0.810.81 vs 7575) gives the same winner, because the square root never changes which value is smallest. Different source points can also share a match: if both sources sat near t0t_0, both entries of the output would be 00.

Constraints

  • source_points: list of [x, y, z] points
  • target_points: list of [x, y, z] points
  • Return list of indices of closest target points

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.