Foundations to Frontier
Tests

Stereo Vision and Depth Estimation

Two cameras a known distance apart see a nearby point at different image positions. That shift, the disparity d, gives depth directly: Z = f B / d. Stereo matching finds the shift for every pixel. Classical pipelines compute a matching cost (SAD, NCC or census), aggregate it over windows or along scanlines with semi-global matching (SGM, Hirschmüller 2005), pick the best disparity, refine it to sub-pixel accuracy and remove inconsistent pixels with a left-right check. Learned stereo networks (RAFT-Stereo, FoundationStereo) now lead the benchmarks. Monocular networks (MiDaS, Depth Anything, Depth Pro) predict depth from a single image, with a scale that must come from elsewhere. This page covers rectification, the Z² growth of depth error, cost aggregation, occlusions, textureless regions and the evaluation metrics. In the exercises you convert disparity to depth, match a block and refine it to sub-pixel. You then compare block matching and SGM on a synthetic pair. The best window size gave 16% bad pixels and SGM gave 8%. A left-right check cut this to 6% while rejecting 96% of occluded pixels. In a textureless band SGM still got 32% wrong.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Two eyes, one distance

Hold a finger in front of your face and close each eye in turn: the finger jumps, and the closer it is the more it jumps. Stereo cameras use the same effect. After the two images are aligned so that matching points lie on the same row, the sideways shift of each point (its disparity) tells how far away it is: big shift, close; small shift, far. Finding the matching point is the hard part, especially on blank walls and around object edges where one camera sees something the other cannot. The demo shows how quickly depth error grows with distance.

3-D vision lab
1 m50 m10.4 m
At Z = 10 m the disparity is 6.0 px and a 0.25 px matching error gives a depth error of 0.42 m. The error grows with Z²: doubling the depth quadruples it; doubling the baseline or focal length halves it.
Why engineers care

Mars rovers navigate with stereo cameras, cars and drones use stereo for obstacle detection, and stereo satellite pairs (Pléiades, WorldView) produce elevation models of the Earth.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Disparity is larger for…
  2. 2. Depth is proportional to…
  3. 3. Stereo matching is hardest on…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Rectification: warp both images with homographies so that epipolar lines become the same image rows (OpenCV stereoRectify); matching becomes a 1-D search along a row.
  • Triangulation in rectified stereo: Z = f B / d with f in pixels and baseline B in metres; X = (u − cₓ) Z / f. Depth error σ_Z ≈ Z² σ_d / (f B): it grows with the square of the distance. The minimum disparity searched sets the maximum range; the maximum disparity sets the closest range.
  • Matching costs: sum of absolute or squared differences (SAD, SSD), normalised cross-correlation (robust to gain), census transform (Zabih and Woodfill, 1994; robust to lighting), mutual information.
  • Aggregation: square windows trade noise against blurred depth edges (“edge fattening”); adaptive or guided-filter windows; semi-global matching (Hirschmüller, 2005 and 2008) sums 1-D dynamic-programming paths in 4–16 directions with penalties P₁ for 1-pixel disparity changes and P₂ for jumps.
  • Post-processing: winner-takes-all, parabolic sub-pixel refinement, left-right consistency check (occlusions and mismatches), uniqueness ratio, speckle filtering, median filtering, hole filling.
  • Learned stereo: GC-Net (2017) and PSMNet (2018) regularise 3-D cost volumes; RAFT-Stereo (2021) iterates recurrent updates; CREStereo (2022), IGEV-Stereo (2023) and FoundationStereo (2025) generalise across domains. Benchmarks: Middlebury, KITTI 2012/2015, ETH3D.
  • Monocular depth: MiDaS (2020) and DPT (2021) predict affine-invariant relative depth; Depth Anything V1/V2 (2024) trains on millions of pseudo-labelled images; Marigold (2024) fine-tunes a diffusion model; Metric3D v2, UniDepth and Depth Pro (2024) predict metric depth using camera intrinsics. Metrics: AbsRel and δ₁ (share with ratio below 1.25).
  • Active depth: structured light (Kinect, 2010), time-of-flight cameras, active infrared stereo (Intel RealSense) and LiDAR (G4-4).
📝 Quick check (Level 1)
0/3 answered
  1. 1. Rectification makes epipolar lines…
  2. 2. Depth error grows with…
  3. 3. SGM penalises…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Matching, refining and checking disparities

Exercise 1.

  • Disparity to depth and its uncertainty.
  • Block matching for one pixel.
  • Parabolic sub-pixel refinement.
  • The left-right consistency check.

Practice.

  • Calibrate the rig and rectify with OpenCV (stereoCalibrate, stereoRectify, initUndistortRectifyMap); check that matching points lie on the same rows.
  • Start with cv2.StereoSGBM (mode SGBM_3WAY) with census-like costs, a uniqueness ratio and speckle filtering; tune numDisparities to the depth range.
  • For best quality use a pretrained learned matcher (RAFT-Stereo, FoundationStereo) and check it on your own imagery.
  • Choose the baseline from the required range: σ_Z / Z = Z σ_d / (f B).
  • For single images use Depth Anything V2 or Depth Pro; align relative depth to metric with a known scale (LiDAR points, camera height) before measuring anything.
import cv2, numpy as np
L, R = (cv2.imread(f, cv2.IMREAD_GRAYSCALE) for f in ("left_rect.png", "right_rect.png"))
sgbm = cv2.StereoSGBM_create(minDisparity=0, numDisparities=128, blockSize=5, P1=8 * 25, P2=32 * 25,
                             uniquenessRatio=10, speckleWindowSize=100, speckleRange=2, disp12MaxDiff=1,
                             mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY)
disp = sgbm.compute(L, R).astype(np.float32) / 16.0          # fixed-point output with 4 fractional bits
f, B = 721.5, 0.54                                            # KITTI-like focal length (px) and baseline (m)
Z = np.where(disp > 0, f * B / np.maximum(disp, 1e-6), np.nan)
print(np.nanpercentile(Z, [5, 50, 95]))
🐍 Exercise 1: depth, matching and checks
Convert disparity to depth with its uncertainty, match one pixel by SAD block matching, refine to sub-pixel and run a left-right check.
📝 Quick check (Level 2)
0/3 answered
  1. 1. OpenCV's semi-global matcher:
  2. 2. Relative monocular depth must be…
  3. 3. A strong learned stereo matcher (2025):

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why depth error grows as Z². Z = f B / d, so dZ/dd = −f B / d² = −Z² / (f B). A fixed disparity error σ_d therefore gives σ_Z ≈ Z² σ_d / (f B): every doubling of distance quadruples the error, and the relative error σ_Z / Z grows linearly. Range is bought with baseline and focal length.

Window size. A larger window averages more pixels, so noise and repetitive texture matter less, and it can reach texture across a flat area. But it assumes constant disparity inside the window, which fails at depth edges: foreground texture dominates the costs and the foreground “fattens” into the background.

Semi-global matching. SGM approximates the 2-D energy E(D) = Σ C(p, D_p) + Σ P₁ [|D_p − D_q| = 1] + Σ P₂ [|D_p − D_q| above 1] by summing 1-D minimisations along several directions, each solved exactly by dynamic programming in O(W D) per scanline. Smoothness is propagated along long paths, so textureless regions take disparities from their textured surroundings.

Occlusions. Background next to a foreground object may be visible in only one image; no correct match exists. The left-right check computes disparities for both images and keeps a pixel only if the two agree, which detects most occluded pixels and many mismatches.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Relative depth error σ_Z / Z grows…
  2. 2. Large windows cause…
  3. 3. Occluded pixels are detected by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

What does each step of a classical stereo pipeline buy on textured surfaces, depth edges, textureless regions and occlusions?

History.

  • D. Scharstein and R. Szeliski, “A taxonomy and evaluation of dense two-frame stereo correspondence algorithms” (2002).
  • R. Zabih and J. Woodfill, “Non-parametric local transforms for computing visual correspondence” (1994).
  • H. Hirschmüller, “Stereo processing by semiglobal matching and mutual information” (2008; first version 2005).
  • L. Lipson, Z. Teed and J. Deng, “RAFT-Stereo” (2021); L. Yang et al., “Depth Anything V2” (2024); B. Wen et al., “FoundationStereo” (2025).

Exercise 2. A synthetic rectified pair of 64 × 160 pixels. A slanted textured background has disparity 8 to 14 px and contains a textureless band. A textured box in front has disparity 24 and occludes part of the background. Both images get noise of 0.01.

  • Methods: absolute-difference costs over disparities 0–31; block matching with 3, 7 and 15-pixel windows; the 7-pixel window with parabolic sub-pixel refinement; SGM in 4 directions (P₁ = 0.02, P₂ = 0.12) on 3-pixel costs with sub-pixel refinement; SGM plus a 1-pixel left-right check.
  • Measures: share of pixels with error over 1 px (bad-1) on all non-occluded pixels, within 3 px of the box edges, in the textureless band and on the textured background; mean absolute error on the textured background.
method all depth edges textureless textured plane plane MAE (px)
block 3 0.30 0.197 0.924 0.108 1.447
block 7 0.218 0.237 0.927 0.014 0.378
block 15 0.163 0.273 0.658 0.007 0.285
block 7 + sub-pixel 0.216 0.245 0.913 0.014 0.224
SGM + sub-pixel 0.077 0.074 0.316 0.004 0.164
SGM + left-right check 0.057 0.022 0.309 0.0 0.128
  • Windows trade texture against edges: going from 3 to 15 pixels cut textureless errors from 0.92 to 0.66 but raised depth-edge errors from 0.197 to 0.273 (seeds 1 and 2: 0.215 to 0.232 and 0.172 to 0.214).
  • Sub-pixel refinement cut the plane error from 0.378 to 0.224 px without changing bad-1.
  • SGM halved the overall error rate of the best window (0.077 against 0.163; seeds 1 and 2: 0.091 against 0.147 and 0.111 against 0.134).
  • The left-right check kept 93.9% of pixels (seeds 1 and 2: 91.2% and 94.4%) and rejected 95.8% of occluded ones (89.1% and 94.6%).
  • Negative result: SGM still got 31.6% of the textureless band wrong (35.0% and 44.8% on seeds 1 and 2): smoothness propagates disparities from its edges, but a 24-pixel band is wide for 4 paths. Learned matchers and plane priors do better here.
  • Caveat: synthetic pair, absolute-difference costs only, no learned methods.
🐍 Exercise 2 (research): what does each step of a classical stereo pipeline buy on textured surfaces, depth edges, textureless regions and occlusions?
Compare block matching with 3, 7 and 15-pixel windows, sub-pixel refinement, semi-global matching and a left-right check on a synthetic stereo pair.

Stereo and depth in AI, robotics and aerospace

  • Planetary rovers: NASA’s Mars rovers compute stereo range maps from navigation and hazard cameras for autonomous driving; Perseverance runs stereo on a dedicated FPGA.
  • Earth observation: along-track stereo (SPOT, Pléiades, WorldView, ASTER) and satellite photogrammetry pipelines such as S2P produce digital surface models with metre-level accuracy.
  • Robotics and drones: stereo depth cameras (ZED, RealSense) and learned matchers feed obstacle avoidance and grasping; monocular depth models help where a single camera is all there is.

References

  1. D. Scharstein and R. Szeliski (2002). “A taxonomy and evaluation of dense two-frame stereo correspondence algorithms.” IJCV.
  2. H. Hirschmüller (2008). “Stereo processing by semiglobal matching and mutual information.” IEEE TPAMI.
  3. R. Zabih and J. Woodfill (1994). “Non-parametric local transforms for computing visual correspondence.” ECCV.
  4. L. Lipson, Z. Teed and J. Deng (2021). “RAFT-Stereo: multilevel recurrent field transforms for stereo matching.” 3DV.
  5. R. Ranftl et al. (2020). “Towards robust monocular depth estimation: mixing datasets for zero-shot cross-dataset transfer.” IEEE TPAMI (MiDaS).
  6. L. Yang et al. (2024). “Depth Anything V2.” NeurIPS.
  7. B. Wen et al. (2025). “FoundationStereo: zero-shot stereo matching.” CVPR.