PIXELBANKv9.1.0
Menu

Disparity to Depth Conversion

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)

Example:

Input:
disparity_to_depth(10, 500, 0.1)
Output:
5.0
Reasoning:

Computing depth from disparity = 10:

  • Z = (f × B) / d
  • Z = (500 × 0.1) / 10
  • Z = 50 / 10
  • Z = 5.0 meters Object is 5 meters from the camera.

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
solution.py

Test Results

0/0
Run code to see test results.