Disparity vs Depth

For a rectified stereo pair, a 3D point projects to the left and right images at the same row but different columns. The horizontal offset between the two projections is the disparity d=uLuRd = u_L - u_R (in pixels). For focal length ff (pixels) and stereo baseline BB (meters), disparity and depth ZZ are related by

d=fBZZ=fBd.d = \frac{f \cdot B}{Z} \quad\Longleftrightarrow\quad Z = \frac{f \cdot B}{d}.

The relationship is inverse: near objects have large disparity, far objects have small disparity.

Where the formula comes from

Place the left camera at the origin and the right camera at (B,0,0)(B, 0, 0), both looking down +Z+Z with the same focal length ff and principal point cxc_x (which is exactly what rectification arranges). A point (X,Y,Z)(X, Y, Z) projects to

uL=fXZ+cx,uR=fXBZ+cx,u_L = f\,\frac{X}{Z} + c_x, \qquad u_R = f\,\frac{X - B}{Z} + c_x,

so the rows agree (vL=vRv_L = v_R) and the column difference is

d=uLuR=fX(XB)Z=fBZ.d = u_L - u_R = f\,\frac{X - (X - B)}{Z} = \frac{fB}{Z}.

This is also why stereo SLAM systems can store a keypoint as the triple (uL,vL,uR)(u_L, v_L, u_R) — as ORB-SLAM2 does — and use uR=f(XcB)/Zc+cxu_R = f\,(X_c - B)/Z_c + c_x as a third residual row in bundle adjustment: the right-image column is the depth measurement.

The Z2Z^2 law: depth resolution degrades quadratically

Differentiating Z=fB/dZ = fB/d with respect to dd gives

ΔZZ2fBΔd,\Delta Z \approx \frac{Z^2}{fB}\,\Delta d,

so a fixed disparity-matching error Δd\Delta d (typically a fraction of a pixel) produces a depth error that grows with Z2Z^2: doubling the distance quadruples the depth uncertainty. A worked example with f=500f = 500 px, B=0.1B = 0.1 m (so fB=50fB = 50 px·m) and matching precision Δd=0.25\Delta d = 0.25 px:

Depth ZZDisparity d=fB/Zd = fB/ZDepth error ΔZ\Delta Z
2 m25 px0.02 m (1%)
10 m5 px0.5 m (5%)
20 m2.5 px2 m (10%)

At 20 m this rig’s “metric” depth is barely better than a guess — the same measurement that is millimeter-grade at arm’s length.

Design consequences

In the estimator: prefer inverse depth

Note that disparity is linear in inverse depth: with ρ=1/Z\rho = 1/Z,

d=fBρ.d = fB\,\rho.

Pixel-level matching noise therefore maps to approximately Gaussian noise in ρ\rho, but to skewed, heavy-tailed noise in ZZ (a ±0.25\pm 0.25 px error at d=1d = 1 px spans a huge, asymmetric depth interval). This is why SLAM systems parameterize far points by inverse depth, and why measurement covariances should be modeled in disparity/inverse-depth space and propagated, rather than assumed Gaussian in metric depth.

In a SLAM pipeline the conversion appears in two places: turning per-keypoint disparities into 3D landmark positions (or an inverse-depth parameterization), and weighting stereo observations in bundle adjustment according to their depth-dependent uncertainty.

Common pitfalls

Why it matters for SLAM

The d=fB/Zd = fB/Z geometry decides whether stereo actually helps: it tells you which landmarks carry metric information, how to set your rig’s baseline for your operating range, and how to model measurement noise correctly in the estimator. Misunderstanding it (e.g., trusting stereo depth at 100 m from a 10 cm baseline) is a classic source of biased maps and inconsistent covariances.