Skip to content
Snippets Groups Projects
Commit b3b028a8 authored by tee1g21's avatar tee1g21
Browse files

Refactor depth2disparity function for improved stability and clarity

parent 613e29d7
No related branches found
No related tags found
1 merge request!15GDP_4.4.6-Depth2Disparity
...@@ -45,25 +45,32 @@ def depth2disparity(abs_depth_map, baseline=0.176, disp_scale=2.0, disp_offset=- ...@@ -45,25 +45,32 @@ def depth2disparity(abs_depth_map, baseline=0.176, disp_scale=2.0, disp_offset=-
# Retrieve the absolute depth (r_t) for this pixel # Retrieve the absolute depth (r_t) for this pixel
r_t = abs_depth_map[i, j] r_t = abs_depth_map[i, j]
# Avoid division by zero for invalid or infinite depth values # Avoid invalid or infinite depth values
if r_t <= 0: if r_t <= 0:
disparity_map[i, j] = 0 disparity_map[i, j] = 0
continue continue
# Calculate angular disparity (d) using the spherical depth-to-disparity formula
try: try:
angle_disp = np.arctan( # Compute denominator with stability checks
baseline / (r_t * np.sin(theta_t) + r_t * np.cos(theta_t) * np.tan(theta_t)) epsilon = 1e-8 # Small value to prevent division instability
) - theta_t tan_theta_t = np.tan(theta_t) if np.abs(np.cos(theta_t)) > epsilon else np.sign(np.sin(theta_t)) * np.inf
denominator = r_t * np.sin(theta_t) + r_t * np.cos(theta_t) * tan_theta_t
if denominator <= 0:
disparity_map[i, j] = 0
continue
# Calculate angular disparity (d)
angle_disp = np.arctan(baseline / denominator) - theta_t
# Convert angular disparity to pixel disparity # Convert angular disparity to pixel disparity
disparity_map[i, j] = (angle_disp / (unit_h * np.pi) - disp_offset) * disp_scale disparity_map[i, j] = (angle_disp / (unit_h * np.pi) - disp_offset) * disp_scale
except ZeroDivisionError: except ZeroDivisionError:
disparity_map[i, j] = 0 disparity_map[i, j] = 0
return disparity_map return disparity_map
if __name__ == "__main__": if __name__ == "__main__":
# Set up argument parser # Set up argument parser
parser = argparse.ArgumentParser(description="Convert relative depth map to absolute depth map.") parser = argparse.ArgumentParser(description="Convert relative depth map to absolute depth map.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment