Compute Depth Map from Disparity
Given a disparity map, focal length , and baseline , compute the depth map using the stereo depth equation.
In a rectified stereo system, depth is inversely proportional to disparity :
where:
- is the focal length in pixels
- is the baseline distance between cameras
- 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
disparity_map = [[10, 20, 0],
[5, 15, 25]]
focal_length = 500.0
baseline = 0.1[[5.0, 2.5, inf], [10.0, 3.3333, 2.0]]
- Inputs:
disparity_mapis a grid of disparities in pixels: how far each point shifts horizontally between the two images.focal_lengthis pixels andbaselineis , the distance between the two cameras. - Shared numerator: every pixel uses the same product , so each depth is just .
- Top row: and . The third pixel has : no match was found, so its depth is defined as infinity,
inf, rather than dividing by zero. - Bottom row: , which rounds to at 4 decimal places, and .
- Output: the same shape,
[[5.0, 2.5, inf], [10.0, 3.3333, 2.0]]. - Notice: depth is inversely proportional to disparity. Doubling from 10 to 20 halves from 5.0 to 2.5, so the pixel that shifts most () 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