v9.1.1
Menu

Compute Depth Map from Disparity

mediumPRODepth EstimationComputer Vision 1

Given a disparity map, focal length ff, and baseline bb, compute the depth map using the stereo depth equation.

In a rectified stereo system, depth ZZ is inversely proportional to disparity dd:

Z=f⋅bdZ = \frac{f \cdot b}{d}

where:

  • ff is the focal length in pixels
  • bb is the baseline distance between cameras
  • dd is the disparity (difference in x-coordinates between left and right images)

For zero disparity (no correspondence found), set the depth to infinity (float('inf')).

Return the depth map as a 2D list with values rounded to 4 decimal places.

Example

Input
disparity_map = [[10, 20, 0],
                 [5, 15, 25]]
focal_length = 500.0
baseline = 0.1
Output
[[5.0, 2.5, inf], [10.0, 3.3333, 2.0]]
Reasoning
  • Inputs: disparity_map is a 2×32 \times 3 grid of disparities in pixels: how far each point shifts horizontally between the two images. focal_length is f=500f = 500 pixels and baseline is b=0.1b = 0.1, the distance between the two cameras.
  • Shared numerator: every pixel uses the same product f⋅b=500×0.1=50f \cdot b = 500 \times 0.1 = 50, so each depth is just Z=50/dZ = 50 / d.
  • Top row: 50/10=5.050/10 = 5.0 and 50/20=2.550/20 = 2.5. The third pixel has d=0d = 0: no match was found, so its depth is defined as infinity, inf, rather than dividing by zero.
  • Bottom row: 50/5=10.050/5 = 10.0, 50/15=3.3333…50/15 = 3.3333\ldots which rounds to 3.33333.3333 at 4 decimal places, and 50/25=2.050/25 = 2.0.
  • Output: the same 2×32 \times 3 shape, [[5.0, 2.5, inf], [10.0, 3.3333, 2.0]].
  • Notice: depth is inversely proportional to disparity. Doubling dd from 10 to 20 halves ZZ from 5.0 to 2.5, so the pixel that shifts most (d=25d = 25) is the closest point and zero shift means infinitely far.

Constraints

  • disparity_map: 2D list of floats (disparity values >= 0)
  • focal_length: float (focal length in pixels)
  • baseline: float (baseline distance)
  • Return: 2D list of floats (depth values)
  • Zero disparity -> float('inf')
  • Round to 4 decimal places
  • Use pure Python

Learn this in

More from Computer Vision 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.