PIXELBANKv9.1.0
Menu

Compute Depth Map from Disparity

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:
  • The depth map is computed by applying the stereo depth equation to each element in the disparity map: Z=fâ‹…bdZ = \frac{f \cdot b}{d}, where f=500.0f = 500.0, b=0.1b = 0.1, and dd is the disparity value.
  • For each element in the disparity map, we plug in the values into the equation: for the first element, Z=500.0â‹…0.110=5.0Z = \frac{500.0 \cdot 0.1}{10} = 5.0; for the second element, Z=500.0â‹…0.120=2.5Z = \frac{500.0 \cdot 0.1}{20} = 2.5; and so on.
  • When the disparity value is 0 (indicating no correspondence found), the depth is set to infinity: Z=500.0â‹…0.10=∞Z = \frac{500.0 \cdot 0.1}{0} = \infty or float('inf').
  • The computed depth values are then rounded to 4 decimal places and returned as a 2D list, resulting in the output: [[5.0, 2.5, inf], [10.0, 3.3333, 2.0]].

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
🔒

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

0/0
Run code to see test results.
Compute Depth Map from Disparity - Medium | PixelBank