Algorithm Search⌘K

Conceptintro

Depth from disparity

Also called stereo depth equation.

For a rectified pair with focal length f and baseline b, a point with disparity d has depth Z = f b / d, so depth error grows as the square of depth.

In a rectified stereo pair with focal length ff in pixels and baseline bb, a point with disparity dd has depth Z=fb/dZ = f b / d, and its other two coordinates follow from its pixel. This is triangulation in the simplest geometry, where the two rays lie in one plane with the baseline and meet at a point that two similar triangles give. The same formula shows the main limit of stereo: an error of δd\delta d pixels in disparity gives an error of about Z2δd/(fb)Z^2 \delta d / (f b) in depth, which grows as the square of the distance.

In plain words

Hold a finger close to your eyes and close one eye, then the other. The finger jumps a lot against the background. A far tree hardly jumps at all. The jump is the disparity, and it is large for near things and small for far things. With two cameras a known distance apart, the size of the jump gives the distance exactly: half the jump means twice the distance. But far things jump so little that a small error in measuring the jump gives a large error in the distance. So stereo measures near things well and far things poorly, and a wider gap between the cameras helps.

The idea

The formula. Put the first camera at the origin and the second at (b,0,0)(b, 0, 0), both with intrinsic matrix KK with fx=fy=ff_x = f_y = f, principal point (cx,cy)(c_x, c_y) and the same rotation, as after rectification. A point (X,Y,Z)(X, Y, Z) has pixels

u1=fXZ+cx,u2=fX−bZ+cx,v=fYZ+cy.u_1 = f \frac{X}{Z} + c_x, \qquad u_2 = f \frac{X - b}{Z} + c_x, \qquad v = f \frac{Y}{Z} + c_y .

So d=u1−u2=fb/Zd = u_1 - u_2 = f b / Z, and

Z=fbd,X=(u1−cx)Zf=(u1−cx)bd,Y=(v−cy)bd.Z = \frac{f b}{d}, \qquad X = \frac{(u_1 - c_x) Z}{f} = \frac{(u_1 - c_x) b}{d}, \qquad Y = \frac{(v - c_y) b}{d} .

The units of ZZ are the units of bb. The pixel size cancels, because ff and dd are both in pixels. In homogeneous form, (X,Y,Z,W)T=Q(u1,v,d,1)T(X, Y, Z, W)^{\mathsf T} = Q (u_1, v, d, 1)^{\mathsf T} with

Q=(100−cx010−cy000f001/b0)Q = \begin{pmatrix} 1 & 0 & 0 & -c_x \\ 0 & 1 & 0 & -c_y \\ 0 & 0 & 0 & f \\ 0 & 0 & 1/b & 0 \end{pmatrix}

and the point is (X/W,Y/W,Z/W)(X/W, Y/W, Z/W). OpenCV calls this matrix the reprojection matrix; with fx≠fyf_x \ne f_y, the second row gets the factor fx/fyf_x / f_y. If the rectified cameras have different principal points cx1c_{x1} and cx2c_{x2}, replace dd by d−(cx1−cx2)d - (c_{x1} - c_{x2}) first.

The error. Differentiate Z=fb/dZ = f b / d:

∂Z∂d=−fbd2=−Z2fb,∣δZ∣≈Z2fb∣δd∣,∣δZ∣Z≈Zfb∣δd∣.\frac{\partial Z}{\partial d} = -\frac{f b}{d^2} = -\frac{Z^2}{f b}, \qquad |\delta Z| \approx \frac{Z^2}{f b} |\delta d| , \qquad \frac{|\delta Z|}{Z} \approx \frac{Z}{f b} |\delta d| .

So the absolute depth error grows as Z2Z^2, and the relative error grows as ZZ. This is a theorem to first order in δd\delta d. The size of δd\delta d is an empirical fact about the matcher: a good subpixel refinement gets to a few tenths of a pixel on textured surfaces, and much worse on textureless ones.

Depth layers. A matcher that returns whole disparities can only give the depths Zk=fb/kZ_k = f b / k, k=1,2,…k = 1, 2, \ldots The gap between two layers is Zk−Zk+1=fb/(k(k+1))≈Z2/(fb)Z_k - Z_{k+1} = f b / (k(k+1)) \approx Z^2 / (f b), the same law. Near points are in thin layers and far points in thick ones.

The range. A search over 0≤d<D0 \le d < D cannot find points nearer than Zmin⁡=fb/(D−1)Z_{\min} = f b / (D - 1). The far limit is set by the smallest disparity the matcher can tell from zero.

The bias. The map d↦fb/dd \mapsto f b / d is convex for d>0d > 0. So if the measured disparity is the true one plus zero mean noise of variance σ2\sigma^2, the mean of the computed depth is larger than the true depth (Jensen's inequality), by about Zσ2/d2Z \sigma^2 / d^2 to second order. For small disparities, averaging depths is worse than averaging disparities and then converting.

The baseline. A larger bb or a larger ff gives a larger disparity for the same depth and so a smaller depth error. The price is a larger disparity range to search, more occlusion, and a larger change of view between the images, which makes matching harder. Okutomi and Kanade (1993) used several baselines at once to get both.

Examples

Check value. f=500f = 500, b=0.1b = 0.1 m, d=20d = 20 gives Z=500⋅0.1/20=2.5Z = 500 \cdot 0.1 / 20 = 2.5 m. With (cx,cy)=(320,240)(c_x, c_y) = (320, 240) and the pixel (u1,v)=(420,290)(u_1, v) = (420, 290), the point is X=100⋅0.1/20=0.5X = 100 \cdot 0.1 / 20 = 0.5 m, Y=50⋅0.1/20=0.25Y = 50 \cdot 0.1 / 20 = 0.25 m.

Error against depth. With f=500f = 500, b=0.1b = 0.1 m and a disparity error of 0.250.25 pixel:

ZZ (m) dd (pixels) ∣δZ∣≈Z2⋅0.25/50\lvert \delta Z \rvert \approx Z^2 \cdot 0.25 / 50 (m) relative error
1 50 0.005 0.5%
2.5 20 0.031 1.25%
5 10 0.125 2.5%
10 5 0.5 5%
25 2 3.1 12.5%

At 25 m the disparity is 2 pixels, and a quarter pixel of error is already an eighth of it.

Depth layers. With the same rig, whole disparities 20 and 19 give 2.5 m and 2.632 m, a gap of 13 cm. Disparities 5 and 4 give 10 m and 12.5 m, a gap of 2.5 m.

Where it is used

  • Stereo cameras. Robots, cars and drones turn each disparity map from stereo matching into a depth map and a point cloud with this formula. The Z2Z^2 law sets the useful range of a camera pair for a given baseline.
  • Structured light. A projector and a camera form a rectified pair in the same way, and structured light scanning uses the same formula with the projector in place of the second camera.
  • Fusion. Depth map fusion and TSDF fusion weight each depth by its expected error, which grows with Z2Z^2.
  • General triangulation. For cameras that are not rectified, the rays do not meet in general, and point triangulation finds the best point by least squares. The rectified formula is its special case.

References

  1. Richard Szeliski, Computer Vision, Algorithms and Applications, second edition, Springer, 2022, Chapter 12, Depth estimation (the relation between disparity and depth in a rectified pair)
  2. Daniel Scharstein and Richard Szeliski, A taxonomy and evaluation of dense two-frame stereo correspondence algorithms, International Journal of Computer Vision 47(1-3), 2002, 7-42, the part that relates disparity to inverse depth
  3. Richard Hartley and Andrew Zisserman, Multiple View Geometry in Computer Vision, second edition, Cambridge University Press, 2004, Chapter 12, Structure computation (triangulation of a point from two views, of which the rectified case is the simplest)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/V59KRG/text.yaml