Two-view geometry and structure from motion
The fundamental matrix, the essential matrix, recovering the camera motion, and triangulating a 3D point cloud from two photos.
- State the epipolar constraint and estimate F with the eight-point algorithm
- Decompose the essential matrix into rotation and translation, and choose among the four solutions
- Triangulate points and explain why the reconstruction has no absolute scale
A homography only relates two views of a plane. For a general 3D scene, a point seen at \(x_1\) in the first image can appear anywhere along a line in the second — the epipolar line — because its depth is unknown.
Algebraically, every true correspondence satisfies
\[x_2^{\mathsf T} F\, x_1 = 0,\]with \(F\) the 3 × 3 fundamental matrix (rank 2, 7 degrees of freedom). \(F x_1\) is the epipolar line in image 2. With known intrinsics \(K\), the essential matrix \(E = K^{\mathsf T} F K\) encodes the motion itself: \(E = [t]_\times R\).
The eight-point algorithm — and a transposed F
Each correspondence gives one linear equation in the nine entries of \(F\); eight give a null space via the SVD, and setting the smallest singular value to zero enforces rank 2. That is computeF. Its test fails, though: testFundamentalMat reports an error of 1.24 against the reference.
The reason is the row order. Expanding \(x_2^{\mathsf T} F x_1\) with \(x_1 = (p_x, p_y, 1)\), \(x_2 = (q_x, q_y, 1)\) gives coefficients \((q_x p_x,\ q_x p_y,\ q_x,\ q_y p_x,\ q_y p_y,\ q_y,\ p_x,\ p_y,\ 1)\). The code writes \((p_x q_x,\ p_x q_y,\ p_x,\ \dots)\) — the expansion of \(x_1^{\mathsf T} F x_2\) — so it returns \(F^{\mathsf T}\). The transpose of its result matches the reference to \(2 \times 10^{-13}\). Swapping the roles in the row fixes it:
A[i] = [qx*px, qx*py, qx, qy*px, qy*py, qy, px, py, 1] # x2^T F x1 = 0
This is not cosmetic: epipolarConstraint computes the line as F @ p1h and measures p2’s distance to it, so with the transposed \(F\) RANSAC scores the wrong lines. Running the same RANSAC loop (1000 samples, numInliers with its 4-pixel threshold) on the repository’s photo pair at half resolution (317 ORB matches), computeF as written keeps 40 inliers; with the fix, 229. Two more fixes help: A is created with .astype('int'), which truncates real keypoint coordinates, and normalising the coordinates (Hartley) before the SVD improves the conditioning of a matrix whose entries range from 1 to about \(10^6\).
From E to the camera motion
decompose computes \(E = U \Sigma V^{\mathsf T}\) and the two rotations \(U W V^{\mathsf T}\), \(U W^{\mathsf T} V^{\mathsf T}\). The translation is the last column of \(U\), up to sign — but the code writes
s = U[:, 2] / abs(U[:, 2]) # element-wise: a vector of +1 and -1
which keeps only the signs: for the test matrix, \((1, 1, 1)\) instead of \((0.997, 0.046, 0.056)\). testDecompose prints nothing for the translation because neither branch of its check matches. t1, t2 = U[:, 2], -U[:, 2] is the fix. Of the four \((R, t)\) combinations exactly one puts the points in front of both cameras; on the photo pair the counts were 0, 0, 229 and 0. relativeTransformation is meant to pick that one, but never updates bestCount, so it returns the last candidate with any point in front rather than the best; adding bestCount = count inside the if fixes it.
Triangulation
Given both projection matrices \(P_1 = K[I \mid 0]\) and \(P_2 = K[R \mid t]\), each image point gives two linear equations in the homogeneous 3D point, and triangulate solves the 4 × 4 system by SVD. It passes its test (error \(5 \times 10^{-6}\)). Running it on SIFT matches of the two photos, with the pose from OpenCV’s essential-matrix solver, gives a 375-point cloud with a median reprojection error of 0.18 px:
The repository’s own pipeline with the fixes above recovers a rotation of 22.6°, against 21.9° from OpenCV on the same matches — a gap of the size that different inlier sets and thresholds produce (OpenCV uses 1 px and a five-point solver).
Two views cannot tell a small scene from a large one far away: the cloud is measured in units of the baseline, \(\vert t\vert = 1\). One known distance in the scene — the box’s width, say — fixes the scale.
Try it yourself. Apply the one-line row fix to computeF and confirm testFundamentalMat passes. Then compute the epipolar line F @ p1h for a few inlier matches and draw it on image 2 with cv2.line — each matching point should sit on its line. Finally, colour the point cloud correctly: the main script indexes inlier_points1[i] after filtering wps, so colours and points drift apart.