You can estimate distance from a single camera by using the pinhole projection formula: distance = (real height × focal length) ÷ pixel height, where focal length is in pixels. This works for any object of known real-world size, such as a person, a road sign, or a license plate. The accuracy depends on how precisely you know the focal length and the object's true size, so calibrate first and expect errors of roughly 5–15% in typical outdoor scenes.
How the Pinhole Formula Turns Pixels Into Meters
The camera model treats light as rays passing through a single point. A point at distance Z meters and height Y meters projects onto the sensor at pixel height y according to y = f × Y / Z, where f is the focal length in pixels. Rearranging gives Z = f × Y / y. The only unknown in a live frame is y, which you measure by drawing a bounding box around the object and reading its height in pixels.
Focal length in pixels is not the number printed on the lens. It equals f_mm × image_width_px ÷ sensor_width_mm. For a camera with a 4.0 mm lens, a 6.17 mm wide sensor, and 1920-pixel-wide images, f = 4.0 × 1920 ÷ 6.17 ≈ 1245 pixels. If you do not know the sensor width, calibrate instead: place an object of known height at a known distance, measure its pixel height, and solve f = y × Z ÷ Y.
Worked example: a person 1.75 m tall appears 120 pixels tall in a frame with f = 1245 px. Distance = 1245 × 1.75 ÷ 120 ≈ 18.2 m. If the same person appears 60 pixels tall, the estimate doubles to about 36.4 m. The formula is linear in pixel height, so small measurement errors at long range cause large distance errors: a 2-pixel error on a 60-pixel box is a 3.3% error, but on a 20-pixel box it is 10%.
How to Calibrate Focal Length in Pixels
Print a target of known height, such as a 1 m by 1 m checkerboard or a measured strip of tape on a wall. Place the camera on a tripod and the target at a measured distance between 3 m and 10 m, square to the optical axis. Capture an image, measure the target's height in pixels with an image editor, and compute f = pixel_height × distance ÷ real_height. Repeat at three distances and average the results; if the values differ by more than 5%, check that the target is not tilted and that you measured to the sensor plane, not the lens front.
Record the resolution you calibrated at. If you later resize frames, f scales with the resize factor: halving image width halves f in pixels. Store f alongside the resolution in your code so the formula stays consistent.
Which Objects Give Reliable Known Heights
Choose objects whose size is standardized or easy to verify. People are convenient but vary widely in height; using an assumed 1.70 m for adults introduces errors of roughly ±10% because real heights range from about 1.5 m to 1.95 m. Road signs, license plates, traffic cones, and standard door frames are more consistent. A US license plate is 12 inches (30.5 cm) tall; a standard interior door is about 80 inches (203 cm) tall. Measure the specific object class you plan to use rather than trusting memory.
For vehicles, class-specific averages work better than one global number: a sedan is roughly 1.45–1.55 m tall, an SUV 1.65–1.85 m, and a box truck 3.0–3.8 m. Classify first, then apply the matching height. For pedestrians, estimate height from a ground-plane assumption or use a fixed value and accept the spread.
Ground-Plane Method for People and Vehicles
When the object stands on flat ground and the camera is at a known height, distance can be computed from the vertical pixel position of the object's base rather than its height. The relation is Z = f × H_cam ÷ (y_base − y_horizon), where H_cam is camera height above the ground, y_base is the pixel row where the object touches the ground, and y_horizon is the pixel row of the horizon. This avoids dependence on object height and works even for objects of unknown size.
Find y_horizon by pointing the camera level and reading the row at the optical center, or by calibrating against two known ground points. Keep the camera height fixed; a 10 cm error in H_cam produces roughly a 10% distance error. This method fails on slopes and over water, where the ground plane assumption breaks.
Comparing Single-Camera Methods
| Method | Inputs needed | Typical accuracy | Best use |
|---|---|---|---|
| Pinhole height formula | Focal length in px, real object height, pixel height | 5–15% | Known-size objects at medium range |
| Ground-plane projection | Camera height, horizon row, base pixel row | 5–20% | People and vehicles on flat ground |
| Monocular depth model | Single image, trained network | Relative depth only, no absolute scale | Scene layout, obstacle ordering |
| Motion and time-to-contact | Two or more frames, ego speed | Depends on speed accuracy | Collision warning, following distance |
Learned monocular depth models output relative depth maps, not meters, unless trained with metric supervision or fused with a known scale. Treat their output as ordering information and convert to meters only with an independent scale reference.
How to Measure Pixel Height Accurately
Draw the bounding box tightly around the object, from the top of the head to the soles for a person, and from the roof to the tire contact patch for a vehicle. Include the full object; clipping the feet shortens the box and overestimates distance. Use the same landmarks at every range so the measurement is consistent.
At long range the object may be only 10–30 pixels tall, where a 1-pixel error is 3–10%. Upscale the crop before measuring, or track the box over several frames and average. Reject frames where the object is partially occluded, motion-blurred, or cut by the image border.
Correcting for Lens Distortion and Tilt
Wide-angle lenses bend straight lines near the edges, which shifts pixel heights by several percent. Calibrate the camera with a checkerboard to obtain distortion coefficients, then undistort frames before measuring. If you cannot calibrate, keep the object near the image center, where distortion is smallest.
Camera tilt changes the effective vertical scale. A camera pitched down by 10 degrees compresses vertical pixel measurements slightly; for accurate work, record pitch and roll from an inertial sensor and correct the projection, or mount the camera level. Rolling shutter cameras also skew fast-moving objects, so measure stationary or slow objects when possible.
What Accuracy to Expect and How to Improve It
Single-camera distance estimates are typically within 5–15% at ranges under about 30 m for known-size objects, and degrade quickly beyond that as pixel counts shrink. To improve accuracy, calibrate focal length at the working resolution, use objects with verified dimensions, measure boxes consistently, undistort frames, and average estimates over multiple frames.
Report a range rather than a single number: if the pixel height is uncertain by ±2 pixels, compute distance at both extremes and present the interval. For safety-related uses such as following distance or collision warning, treat the estimate as advisory and combine it with radar, ultrasonic sensors, or time-to-contact from frame-to-frame scaling, which does not require knowing the object's size.