v9.1.1
Menu

Triangulate 3D Point from Two Views

hardPROMultiple CamerasComputer Vision 1

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
  • Inputs: P1=[I∣0]P_1 = [I \mid 0]; P2P_2 is the same camera shifted one unit along xx. The point is seen at (0.5,0.5)(0.5, 0.5) and (0.0,0.5)(0.0, 0.5).
  • Rows of AA: with P3T=[0,0,1,0]P^{3T} = [0, 0, 1, 0] in both cameras, the four rows are [−1,0,0.5,0][-1, 0, 0.5, 0], [0,−1,0.5,0][0, -1, 0.5, 0], [−1,0,0,1][-1, 0, 0, 1] and [0,−1,0.5,0][0, -1, 0.5, 0].
  • Solving AX=0A\mathbf{X} = 0 with X=[X,Y,Z,W]\mathbf{X} = [X, Y, Z, W]: row 1 gives Z=2XZ = 2X, row 2 gives Y=Z/2Y = Z/2, row 3 gives W=XW = X. Every solution is a multiple of [1,1,2,1][1, 1, 2, 1]; the unit-length one found is ≈−0.378⋅[1,1,2,1]\approx -0.378 \cdot [1, 1, 2, 1].
  • Homogeneous to Euclidean: dividing the first three entries by WW cancels both the scale and the sign, giving [1.0, 1.0, 2.0].
  • Notice that rows 2 and 4 are identical (the cameras differ only along xx, so vv agrees), so AA has rank 3 and the null space is exact. With noisy points, AA has full rank and you get the best least-squares fit instead.

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

More from Computer Vision 1

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.