v9.1.1
Menu

Disparity to Depth Conversion

easyFREEStereo MatchingStereoCV: Depth Estimation

Convert stereo disparity to metric depth using the stereo geometry equation.

In a rectified stereo setup, the relationship between disparity dd (in pixels) and depth ZZ (in world units) is:

Z=f⋅BdZ = \frac{f \cdot B}{d}

where:

  • ff is the focal length in pixels
  • BB is the baseline (distance between camera centers) in world units
  • dd is the disparity (difference in x-coordinates between matched pixels)

Key observations:

  • Depth is inversely proportional to disparity
  • Close objects have high disparity (appear shifted more)
  • Far objects have low disparity
  • Zero disparity means infinite depth (divide by zero)

Return the depth rounded to 4 decimal places, or infinity (inf) when the disparity is 00.

Example

Input
disparity_to_depth(10, 500, 0.1)
Output
5.0
Reasoning
  • Inputs: disparity d=10d = 10 pixels (how far the matched point shifts between the left and right images), focal f=500f = 500 pixels, and baseline B=0.1B = 0.1 metres between the two camera centres.
  • Numerator: f⋅B=500×0.1=50f \cdot B = 500 \times 0.1 = 50. Its units are pixel·metres, so dividing by a disparity in pixels leaves metres.
  • Divide by disparity: Z=5010=5.0Z = \frac{50}{10} = 5.0, so the point sits 5 metres from the cameras. The answer is in the baseline's units because pixels cancel.
  • Output: a single float, 5.0, rounded to 4 decimal places.
  • Notice that depth is inversely proportional to disparity: doubling dd to 20 halves the depth to 2.52.5, and halving it to 5 doubles it to 10.010.0. As d→0d \to 0 the depth grows without bound, which is why a disparity of exactly 00 must give infinity rather than a division error.

Constraints

  • disparity: pixel disparity value (can be 0)
  • focal: focal length in pixels
  • baseline: distance between cameras in meters (or other unit)
  • Return depth in same units as baseline, or inf if disparity is 0

Learn this in

More from CV: Depth Estimation

solution.py

Test results

Sign in to run your code against the test cases.