From b3b028a86d59788b57221bb0af1ba5348fc0df71 Mon Sep 17 00:00:00 2001 From: tee1g21 <92794120+tee1g21@users.noreply.github.com> Date: Thu, 12 Dec 2024 19:20:16 +0000 Subject: [PATCH] Refactor depth2disparity function for improved stability and clarity --- scripts/Depth2Disparity/depth2disparity.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/Depth2Disparity/depth2disparity.py b/scripts/Depth2Disparity/depth2disparity.py index 948b347..08c6bb2 100644 --- a/scripts/Depth2Disparity/depth2disparity.py +++ b/scripts/Depth2Disparity/depth2disparity.py @@ -24,7 +24,7 @@ def relative2abs(rel_depth_map, coord1, dist1, coord2, dist2): return abs_depth_map def depth2disparity(abs_depth_map, baseline=0.176, disp_scale=2.0, disp_offset=-120): - + # Get image dimensions height, width = abs_depth_map.shape @@ -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 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: disparity_map[i, j] = 0 continue - # Calculate angular disparity (d) using the spherical depth-to-disparity formula try: - angle_disp = np.arctan( - baseline / (r_t * np.sin(theta_t) + r_t * np.cos(theta_t) * np.tan(theta_t)) - ) - theta_t + # Compute denominator with stability checks + epsilon = 1e-8 # Small value to prevent division instability + 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 disparity_map[i, j] = (angle_disp / (unit_h * np.pi) - disp_offset) * disp_scale + except ZeroDivisionError: disparity_map[i, j] = 0 return disparity_map - if __name__ == "__main__": # Set up argument parser parser = argparse.ArgumentParser(description="Convert relative depth map to absolute depth map.") -- GitLab