A camera squashes the 3-D world onto a flat image. A simple model tells us exactly how, and lets a robot measure distance with one camera.
Key idea
In the pinhole model, an object of height $H$ at distance $Z$ makes an image $h = \dfrac{f\,H}{Z}$ pixels tall, where $f$ is the focal length measured in pixels. Know the object's real size, and $Z = \dfrac{f\,H}{h}$.
Why it works
Light from the top and bottom of the object passes through the pinhole and makes two similar triangles, one outside the camera and one inside. Similar triangles have equal ratios: $h/f = H/Z$. The Math Ladder's similar-triangles topic is all you need.
Worked example: How far?
A 0.2 m ball looks 50 pixels wide, with $f = 600$.
- $Z = 600 \times 0.2 / 50 = 2.4\ \text{m}$.
Answer: 2.4 m.
Common mistake
Mixing units: the focal length is in pixels, and the size and distance must both be in meters (or both in centimeters).
Real lenses bend straight lines a little; camera calibration measures $f$ and that bending from photos of a checkerboard.