Compute Depth Map from Disparity
Given a disparity map, focal length f, and baseline b, compute the depth map using the stereo depth equation.
In a rectified stereo system, depth Z is inversely proportional to disparity d:
Z=df⋅b​
where:
- f is the focal length in pixels
- b is the baseline distance between cameras
- d 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]]
- The depth map is computed by applying the stereo depth equation to each element in the disparity map: Z=df⋅b​, where f=500.0, b=0.1, and d is the disparity value.
- For each element in the disparity map, we plug in the values into the equation: for the first element, Z=10500.0⋅0.1​=5.0; for the second element, Z=20500.0⋅0.1​=2.5; and so on.
- When the disparity value is 0 (indicating no correspondence found), the depth is set to infinity: Z=0500.0⋅0.1​=∞ 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
Background Knowledge
The problem revolves around the concept of depth estimation in computer vision, specifically using stereo vision. Stereo vision is a technique used to estimate the depth of a scene by analyzing the disparity between two images taken from slightly different viewpoints, typically from a stereo camera setup. The disparity map, which is given in the problem, represents the difference in x-coordinates between corresponding points in the left and right images.
The stereo depth equation, Z=df⋅b​, is fundamental to understanding how depth is calculated from disparity. Here, Z is the depth of the point, f is the focal length of the camera in pixels, b is the baseline distance between the cameras, and d is the disparity. This equation shows that depth is inversely proportional to disparity, meaning that as disparity increases (objects appear closer in the images), the calculated depth decreases, indicating the object is closer to the camera.
Understanding the parameters involved is crucial. The focal length f affects how much the camera "zooms" into the scene, the baseline b affects the stereo effect and thus the depth perception, and the disparity d is the measurable difference that we use to calculate depth. In real-world applications, accurately calculating these values is essential for tasks like robotics, autonomous vehicles, and 3D reconstruction.
Algorithm/Approach
The general approach to solving this problem involves applying the stereo depth equation to each pixel in the disparity map. This requires iterating over the disparity map, applying the equation for each non-zero disparity value to calculate the depth, and handling the case where disparity is zero (indicating no correspondence and thus infinite depth).
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.