PIXELBANKv9.1.0
Menu

Triangulate 3D Point from Two Views

Given two camera projection matrices P1P_1 (3x4) and P2P_2 (3x4), and corresponding 2D points x1=[u1,v1]\mathbf{x}_1 = [u_1, v_1] and x2=[u2,v2]\mathbf{x}_2 = [u_2, v_2] in each image, triangulate the 3D point X=[X,Y,Z]\mathbf{X} = [X, Y, Z] using the Direct Linear Transform (DLT) method.

Algorithm (DLT Triangulation):

For each 2D point x=[u,v]\mathbf{x} = [u, v] and its projection matrix PP, we have: x×(P⋅X)=0\mathbf{x} \times (P \cdot \mathbf{X}) = 0

This gives us a system of linear equations. From each view, we extract two independent equations:

ui⋅Pi3T−Pi1T=0u_i \cdot P_i^{3T} - P_i^{1T} = 0 vi⋅Pi3T−Pi2T=0v_i \cdot P_i^{3T} - P_i^{2T} = 0

where PijTP_i^{jT} is the jj-th row of PiP_i.

Build the 4×44 \times 4 matrix AA:

A=(u1P13T−P11Tv1P13T−P12Tu2P23T−P21Tv2P23T−P22T)A = \begin{pmatrix} u_1 P_1^{3T} - P_1^{1T} \\ v_1 P_1^{3T} - P_1^{2T} \\ u_2 P_2^{3T} - P_2^{1T} \\ v_2 P_2^{3T} - P_2^{2T} \end{pmatrix}

Solve via SVD of AA: the solution is the last column of VV (last row of VTV^T), converted from homogeneous coordinates.

Round each coordinate to 4 decimal places.

Example:

Input:
P1 = [[1, 0, 0, 0],
      [0, 1, 0, 0],
      [0, 0, 1, 0]]
P2 = [[1, 0, 0, -1],
      [0, 1, 0, 0],
      [0, 0, 1, 0]]
x1 = [0.5, 0.5]
x2 = [0.0, 0.5]
Output:
[1.0, 1.0, 2.0]
Reasoning:
  • We first construct the 4×44 \times 4 matrix AA using the given projection matrices P1P_1, P2P_2, and the corresponding 2D points x1=[0.5,0.5]\mathbf{x}_1 = [0.5, 0.5] and x2=[0.0,0.5]\mathbf{x}_2 = [0.0, 0.5].
  • The matrix AA is built by extracting two independent equations from each view: uiâ‹…Pi3T−Pi1T=0u_i \cdot P_i^{3T} - P_i^{1T} = 0 and viâ‹…Pi3T−Pi2T=0v_i \cdot P_i^{3T} - P_i^{2T} = 0, resulting in A=(0.5â‹…[0,0,1,0]−[1,0,0,0]0.5â‹…[0,0,1,0]−[0,1,0,0]0.0â‹…[0,0,1,0]−[1,0,0,−1]0.5â‹…[0,0,1,0]−[0,1,0,0])A = \begin{pmatrix} 0.5 \cdot [0, 0, 1, 0] - [1, 0, 0, 0] \\ 0.5 \cdot [0, 0, 1, 0] - [0, 1, 0, 0] \\ 0.0 \cdot [0, 0, 1, 0] - [1, 0, 0, -1] \\ 0.5 \cdot [0, 0, 1, 0] - [0, 1, 0, 0] \end{pmatrix}.
  • We then solve for the 3D point X\mathbf{X} by finding the last column of VV (last row of VTV^T) via SVD of AA, which gives us the solution in homogeneous coordinates.
  • The final output is obtained by converting the solution from homogeneous coordinates to Euclidean coordinates and rounding each coordinate to 4 decimal places, resulting in X=[1.0,1.0,2.0]\mathbf{X} = [1.0, 1.0, 2.0].

Constraints:

  • P1, P2: 3x4 projection matrices as lists of lists
  • x1, x2: 2D points as [u, v]
  • Use numpy for SVD
  • Return: [X, Y, Z] rounded to 4 decimal places
  • The 3D point is in front of both cameras
🔒

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

0/0
Run code to see test results.
Triangulate 3D Point from Two Views - Hard | PixelBank