3D visionExplainer

Measure obstacle distance from a single camera with two frames

One camera can range an obstacle if it moves: two frames with a known sideways shift, Z = f·B/d, a worked example and the accuracy to expect at each distance.

LumaSpec Vision11 min read Share on LinkedIn

On this page 10 sections
  1. 01Can one camera measure distance to an obstacle?
  2. 02What is the two-frame distance formula?
    1. Worked example: a 100 mm sidestep and a 50 px slide
  3. 03How accurate is a distance measured from two frames?
  4. 04How far apart should the two frames be?
  5. 05Does the camera have to slide sideways, or can it rotate?
  6. 06What breaks the match between two frames?
  7. 07When should you fit a distance sensor instead?
  8. 08Which 3D camera does this in a single frame?
  9. 09How do you set up and check the first measurement?
  10. 10Questions

You have one camera on the rig and the task now asks for a number: how far away is that pallet leg, that tote, that person stepping into the cell? A single frame cannot give a metric range. Perspective turns distance into size, and the size is unknown until the distance is known. The way out costs one motion: take a frame, translate the camera sideways by a measured amount, take a second frame, and triangulate from how far the same feature slid across the image. Below is the formula with units that cancel cleanly, a worked example with every step of the arithmetic, the accuracy to expect at each range, the ways the match fails, and the point at which a laser or a 3D camera is the better tool.

Can one camera measure distance to an obstacle?

Yes, if the camera translates between frames: take frame one, shift the camera sideways by a known baseline, take frame two, and triangulate the obstacle distance from the parallax of the same feature, because one stationary frame shows shape and brightness but never metric range.

A camera that never moves sees a flat projection. You can detect the obstacle, track it, even guess nearness by assuming a real-world size, and every such estimate collapses the moment the assumption is wrong. The depth map from a single image guide sets out how far those single-frame estimates reach. Two views from two positions restore the scale: the same amount of camera travel slides a close obstacle across many pixels and a far obstacle across few, and that ratio is the measurement.

What is the two-frame distance formula?

Distance equals focal length in pixels times baseline divided by disparity, Z = f · B / d: with f = 500 px, a baseline B of 100 mm and a 50 px disparity, the obstacle sits at Z = 500 × 100 ÷ 50 = 1,000 mm.

Z = f · B / d f = focal length in pixels = focal length (mm) / pixel pitch (mm) B = baseline, the lateral shift between the two frames (mm) d = disparity, the shift of the feature between the frames (pixels) dZ = Z² · dpx / (f · B) depth uncertainty for one pixel of matching error

The pixels sit above and below the line and cancel, so Z comes out in whichever unit you used for B. One conversion remains: focal length in millimetres divided by pixel pitch in millimetres gives f in pixels, and a 2.8 mm lens on 5.6 µm pixels lands at f = 2.8 ÷ 0.0056 ≈ 500 px. Why this unit looks odd is a small story of its own — why focal length is written in pixels walks through it — and the lens focal length calculator returns f in millimetres from sensor and working distance.

Worked example: a 100 mm sidestep and a 50 px slide

A compact board camera delivers 640-pixel-wide frames and calibrates to f = 500 px, about a 65° horizontal field of view. It rides on a gantry that can slide sideways.

  1. Frame 1: the corner of a bolt head sits at x1 = 380 px from the left edge.
  2. Slide the camera 100 mm to the left.
  3. Frame 2: the same corner now sits at x2 = 430 px. Stationary objects slide right when the camera slides left, so d = 430 - 380 = 50 px.
  4. Distance: Z = 500 px × 100 mm ÷ 50 px = 1,000 mm.
  5. Depth uncertainty: one pixel of matching error gives dZ = 1,000² ÷ 50,000 = 20 mm.
  6. Lateral resolution: one pixel spans Z ÷ f = 1,000 ÷ 500 = 2 mm at this range.
  7. Same bolt at 2,000 mm: d = 500 × 100 ÷ 2,000 = 25 px and dZ = 2,000² ÷ 50,000 = 80 mm.

Read the units once: f · B / d carries px × mm ÷ px, the pixels cancel, and the answer inherits the millimetres of the baseline. Doubling the range doubles the relative error and quadruples the absolute one — the next section puts numbers on that.

How accurate is a distance measured from two frames?

Depth uncertainty follows dZ = Z² · dpx / (f · B), so with f = 500 px, B = 100 mm and one pixel of matching error it is 20 mm at 1,000 mm, 80 mm at 2,000 mm and 180 mm at 3,000 mm.

Distance Z Disparity d Depth error per pixel Relative error
500 mm 100 px 5 mm 1 %
1,000 mm 50 px 20 mm 2 %
2,000 mm 25 px 80 mm 4 %
3,000 mm 17 px 180 mm 6 %

Every row assumes the same f = 500 px, that same baseline and a one-pixel matching error. Relative error is dpx ÷ d, which reduces to Z ÷ (f · B): double the focal length in pixels or double the baseline and every row halves. Matching skill sets dpx — normalized cross-correlation on a textured target typically lands within a few tenths of a pixel, while a smooth target can pull the match several pixels off and still score well. Purpose-built stereo heads meet the same square law, which is why long-range designs reach for a wider baseline; frames from two separate cameras instead of one moved camera need the depth scale read per camera.

How far apart should the two frames be?

Size the baseline so the expected disparity lands between roughly 10 and 100 pixels: B = d · Z / f, which at f = 500 px and a 1,000 mm obstacle means a lateral shift between 20 mm and 200 mm, with the 100 mm sidestep above a sensible default.

Below about 10 px of disparity, one pixel of matching error is already 10 % of the reading and depth noise dominates. Above about 100 px, the feature crosses most of the frame, risks leaving it or slipping behind an occluder, and the object's own relief changes its appearance enough to blur the template match. It is the same trade a stereo head faces with its fixed sensor gap: choosing a stereo camera baseline distance works through the details, and the bounds transfer straight to a camera you move yourself.

Does the camera have to slide sideways, or can it rotate?

It must translate: a pure rotation about the lens center carries no depth information because the whole image swings rigidly and every feature moves by nearly the same amount, so only lateral motion creates parallax that differs with distance.

Keep the optical axis parallel between frames — no yaw, no tilt — and step perpendicular to the viewing direction, or the simple formula stops applying. Driving straight at the object is the other dead end: forward motion changes apparent size rather than disparity, and recovering range from size change needs the exact travel plus the object's true dimensions, more unknowns than a sidestep costs.

What breaks the match between two frames?

Matching needs image structure — corners, edges, printed marks — and a blank wall, a polished casting or a wandering glossy highlight defeats it, producing the classic failure: a confident distance number that is simply wrong.

The usual culprits and their fixes:

  • Untextured surface: nothing repeatable to correlate. Add texture — a decal, a dab of paint, a projected pattern — or change sensing method.
  • Specular highlights: bright spots that move with the viewpoint, not the part. Diffuse the lighting or exclude saturated pixels from the match.
  • Occlusion: at large baselines a near part hides different background in each frame. Match on the part, not on its edge against the background.
  • Focus and blur: both frames must be sharp at one focus setting; refocusing between frames quietly changes magnification.
  • Repeating patterns: mesh, threads and grilles can lock one period off, putting d wrong by exactly one period.

Then verify before acting: match five well-spread features, compute five distances and require agreement within about dZ. Agreement means the geometry held; scatter means rotation crept in or a patch mismatched.

When should you fit a distance sensor instead?

Switch hardware when you need one dependable range to a single point at frame rate with little compute, or when the target has no texture: a time-of-flight laser sensor returns distance directly, and a photoelectric sensor settles plain presence with a simpler part.

If the question is whether anything is parked where the gripper swings, a diffuse-reflective sensor answers outright. The MV-PE5502 covers a 100 mm to 1 m sensing range with a settable switching distance, a red LED and an NPN output. Wired to the camera trigger line it starts the inspection itself — PNP versus NPN trigger wiring covers the hook-up, and the trigger sensor finder sorts the range by output, light source and sensing distance. The photoelectric sensors category adds through-beam and retro-reflective types for other mounts.

Photoelectric sensorsHikrobot MV-PE5502 Diffuse-reflective sensor(10-350mm, Red LED, settable distance, NPN)MV-PE5502
Output
NPN
Light source
Red LED
Sensing range
100 mm - 1 m
Type
Diffuse-reflective
$49.00 · quotedView and quoteBuild a system with it

When the question is the distance itself, a point laser displacement sensor such as the MV-DD020-03H times its own reflected light and reports range to one spot — no texture required, no second viewpoint, no matching code to maintain.

Photoelectric sensorsHikrobot MV-DD020-03H Point laser displacement sensingMV-DD020-03HPrice on requestView and quoteBuild a system with it

Which 3D camera does this in a single frame?

Buy the triangulation ready-made when the camera cannot move: a stereo or structured-light 3D camera computes a full depth map every frame from a fixed, factory-calibrated baseline, and a time-of-flight camera times the returned light directly.

The mathematics is the same as your two frames, with two differences that matter on a machine: both exposures happen at once, so vibration and travel stop corrupting the baseline, and structured-light models project their own pattern, which rescues untextured targets. For the stay-2D-or-go-3D call on a conveyor, see 2D or 3D camera for conveyor pick and place. The 3D camera finder filters the 3D sensors by type and series, and more 3D vision guides collect the follow-on calibration and point-cloud reading. Three families cover most cells:

3D sensorsHikrobot MV-DPS200P-02 Projected structured-light 3D cameraMV-DPS200P-02
Type
Stereo / structured light
$8,829.00 · quotedView and quote
3D sensorsHikrobot MV-DT01WMU ToF sensing cameraMV-DT01WMU
Type
Depth / ToF camera
Price on requestView and quote
3D sensorsHikrobot MV-DLS300P-04 Laser galvanometer stereo cameraMV-DLS300P-04
Type
Stereo / structured light
$16,579.00 · quotedView and quote

The MV-DPS200P-02 projects its own structured-light pattern, the MV-DT01WMU is a time-of-flight sensing camera, and the MV-DLS300P-04 is a laser galvanometer stereo camera that steers its laser across the scene. Match the family to the working volume before matching a model.

How do you set up and check the first measurement?

Calibrate f in pixels from one target of known size at a known distance and verify the range at three distances before trusting a reading: a target spanning 100 pixels at 500 mm gives f = 100 × 500 ÷ 100 = 500 px.

  • Print or machine a target with one measurable feature, a black square of known side, and measure the print with a ruler rather than trusting the label: every error here becomes a proportional error in every distance.
  • Place the target square to the camera at a taped-out distance, count its width in pixels, and compute f = pixels × distance ÷ true width.
  • Give the camera a clean lateral step — a slide, a spare axis or the robot itself — and measure the step physically once.
  • Capture both frames at rest at one exposure and focus; if the rig moves continuously, trigger from position rather than from time.
  • Match five features, compute five distances and act only when they agree within about dZ.
  • Check against a tape at the nearest, middle and farthest working distance, and accept the set-up when each error stays inside dZ.

When a reading misbehaves, suspect the two fudged numbers first: a nominal rather than effective focal length, and a commanded rather than true baseline. Finding sensor size and focal length with no data sheet recovers both from a single test image when the paperwork is missing.

The decision, then. A rig that can make a clean lateral step in the 20-200 mm window and a target that holds texture: two frames and the formula, costing one motion. No movement available, an untextured target, or a need for a full depth map instead of one range: one of the sensors above. Either way, the configurator below walks the rest of the chain, and the contact page reaches the applications team if you would rather send the geometry first.

Build the whole station

The configurator matches a camera, lens, light, light controller and cables to each other, step by step, and lists only parts that fit what you already picked.

Start with the camera

Questions

How do I find the disparity between two frames in practice?

Crop a small patch around the feature in frame one, slide it across frame two in one-pixel steps, and score every position with normalized cross-correlation; the peak gives the pixel shift d. Keep the search along the x axis only, because a purely lateral camera move causes no vertical shift, then refine around the peak for sub-pixel d.

Can I take the two frames while the robot is moving?

Only if every exposure fires at a known position, for example from an encoder pulse, so the travel between frames is exact. Motion blur corrupts the match, so keep exposures short, and remember the formula needs translation between frames: rotation alone carries no range.

Is taking more than two frames worth it?

Yes. Capture frames at several measured baselines, compute a disparity for each, and fit the straight line d = (f / Z) · B: the slope hands you f / Z and the scatter shows your real matching noise. Averaging also cuts random disparity error by about the square root of the number of frames.

What distance error should I budget at 3 m with the example numbers?

With f = 500 px and B = 100 mm, the disparity at 3,000 mm is 500 × 100 ÷ 3,000, about 17 px, so one pixel of matching error is roughly 6 % of the range, or 180 mm. Budget more for smooth targets and confirm with a tape at that distance.