From 0240142df25a5033e99196bebe07213ebf527c83 Mon Sep 17 00:00:00 2001 From: tee1g21 <92794120+tee1g21@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:33:17 +0000 Subject: [PATCH] Final version of depth2disparity --- scripts/Depth2Disparity/depth_to_disparity.py | 178 +++++++++--------- scripts/simple_tab.py | 19 +- 2 files changed, 93 insertions(+), 104 deletions(-) diff --git a/scripts/Depth2Disparity/depth_to_disparity.py b/scripts/Depth2Disparity/depth_to_disparity.py index 10c0df9..e329440 100644 --- a/scripts/Depth2Disparity/depth_to_disparity.py +++ b/scripts/Depth2Disparity/depth_to_disparity.py @@ -64,13 +64,13 @@ class Depth2Disparity: disparity_path = os.path.join(disparity_dir, disparity_base) ## Check if an existing disparity map needs to be renamed - ##if os.path.exists(disparity_path): - ## old_depth_path = os.path.join(disparity_dir, "depth_e_old.png") - ## print(f"Renaming monodepth map to: {old_depth_path}") - ## # if pre-existing 'old depth map' exists, remove it - ## if os.path.exists(old_depth_path): - ## os.remove(old_depth_path) - ## os.rename(disparity_path, old_depth_path) + if os.path.exists(disparity_path): + old_depth_path = os.path.join(disparity_dir, "depth_e_old.png") + print(f"Renaming monodepth map to: {old_depth_path}") + # if pre-existing 'old depth map' exists, remove it + if os.path.exists(old_depth_path): + os.remove(old_depth_path) + os.rename(disparity_path, old_depth_path) # Save the new disparity map print(f"Saving new disparity map to: {disparity_path}") @@ -119,103 +119,93 @@ class Depth2Disparity: return abs_depth_map def _depth2disparity(self, abs_depth_map): - """ - Convert absolute depth map to disparity map for 360-degree images. - - Parameters: - abs_depth_map (numpy.ndarray): Absolute depth map with pixel values as distances in meters. - - Returns: - disparity_map (numpy.ndarray): Disparity map with values representing disparity. - disparity_map_normalized (numpy.ndarray): Normalized disparity map for visualization. - """ - # Define parameters for the conversion height, width = abs_depth_map.shape - - # Latitude grid (θ_l) - epsilon = 1e-6 - latitudes = np.linspace(-np.pi / 2, np.pi / 2, height) - latitudes_grid = np.tile(latitudes[:, np.newaxis], (1, width)) - - # Clamp extreme latitudes to avoid instability - latitudes_grid = np.clip(latitudes_grid, -np.pi/2 + epsilon, np.pi/2 - epsilon) - - plt.imshow(latitudes_grid, cmap="jet") - plt.colorbar() - plt.title("Latitude Grid") - plt.show() - - self.baseline = 0.176 - - - # Calculate angular disparity Δθ(x, y) - # Δθ(x, y) = arctan(B / (r_t * sin(θ_l) + r_t * cos(θ_l) * tan(θ_l))) - θ_l - epsilon = 1e-6 # Small value to prevent instability - denominator = ( - abs_depth_map * np.sin(latitudes_grid) + - abs_depth_map * np.cos(latitudes_grid) * np.tan(latitudes_grid) + - epsilon - ) - angular_disparity = np.arctan(self.baseline / denominator) - latitudes_grid - - self.disp_scale = 10 - self.disp_offset = np.median(angular_disparity) - - # Normalize the disparity map for saving - pixel_angle_scale = 1 # Assume scale factor is 1 if not provided (can be adjusted if needed) - disparity_map = (angular_disparity / pixel_angle_scale - self.disp_offset) * self.disp_scale + unit_h = 1.0 / height + disparity_map = np.zeros_like(abs_depth_map, dtype=np.float32) + + for i in range(height): + theta_t = i * unit_h * np.pi + for j in range(width): + r_t = max(abs_depth_map[i, j], 0.01) # Clamp small depths + if r_t <= 0: + disparity_map[i, j] = 0 + continue + try: + tan_theta_t = np.tan(theta_t) if np.abs(np.cos(theta_t)) > 1e-6 else np.sign(np.sin(theta_t)) * np.inf + denominator = max(r_t * np.sin(theta_t) + r_t * np.cos(theta_t) * tan_theta_t, 1e-6) + angle_disp = np.arctan(self.baseline / denominator) - theta_t + pixel_disp = (angle_disp / (unit_h * np.pi) - self.disp_offset) * self.disp_scale + disparity_map[i, j] = pixel_disp + except ZeroDivisionError: + disparity_map[i, j] = 0 # Normalize for visualization disparity_map_normalized = (disparity_map - disparity_map.min()) / (disparity_map.max() - disparity_map.min()) disparity_map_normalized *= 255 disparity_map_normalized = disparity_map_normalized.astype(np.uint8) - # Debugging visualization - plt.imshow(disparity_map, cmap="jet") - plt.colorbar() - plt.title("Disparity Map (Debugging)") - plt.show() - - return disparity_map, disparity_map_normalized - - def __depth2disparity(self, abs_depth_map): - height, width = abs_depth_map.shape - unit_w = 2.0 / width # Horizontal unit angle - unit_h = 1.0 / height # Vertical unit angle - epsilon = 1e-8 # Small value to prevent instability - - # Latitude angles (theta_t) for each row - theta_t_grid = np.linspace(0, np.pi, height).reshape(height, 1) - - # Ensure no instability in tangent calculation - tan_theta_t = np.tan(np.clip(theta_t_grid, -np.pi/2 + epsilon, np.pi/2 - epsilon)) - - # Broadcast depth map and latitude grid - r_t = abs_depth_map # Absolute depth values (meters) - sin_theta_t = np.sin(theta_t_grid) - cos_theta_t = np.cos(theta_t_grid) - - # Compute denominator - denominator = r_t * sin_theta_t + r_t * cos_theta_t * tan_theta_t + epsilon - - # Avoid invalid denominator values - denominator[denominator <= 0] = epsilon - - # Calculate angular disparity - angle_disp = np.arctan(self.baseline / denominator) - theta_t_grid - - # Convert angular disparity to pixel disparity - disparity_map = (angle_disp / (unit_h * np.pi) - self.disp_offset) * self.disp_scale - plt.imshow(disparity_map, cmap='gray') plt.colorbar() plt.show() - - # Normalize disparity map for visualization - disparity_map_normalized = (disparity_map - disparity_map.min()) / (disparity_map.max() - disparity_map.min()) - disparity_map_normalized *= 255 - disparity_map_normalized = disparity_map_normalized.astype(np.uint8) return disparity_map, disparity_map_normalized - \ No newline at end of file + #def __depth2disparity(self, abs_depth_map): + # """ + # Convert absolute depth map to disparity map for 360-degree images. + + # Parameters: + # abs_depth_map (numpy.ndarray): Absolute depth map with pixel values as distances in meters. + + # Returns: + # disparity_map (numpy.ndarray): Disparity map with values representing disparity. + # disparity_map_normalized (numpy.ndarray): Normalized disparity map for visualization. + # """ + # # Define parameters for the conversion + # height, width = abs_depth_map.shape + # + # # Latitude grid (θ_l) + # epsilon = 1e-6 + # latitudes = np.linspace(-np.pi / 2, np.pi / 2, height) + # latitudes_grid = np.tile(latitudes[:, np.newaxis], (1, width)) + + # # Clamp extreme latitudes to avoid instability + # latitudes_grid = np.clip(latitudes_grid, -np.pi/2 + epsilon, np.pi/2 - epsilon) + + # plt.imshow(latitudes_grid, cmap="jet") + # plt.colorbar() + # plt.title("Latitude Grid") + # plt.show() + + # self.baseline = 0.176 + # + # + # # Calculate angular disparity Δθ(x, y) + # # Δθ(x, y) = arctan(B / (r_t * sin(θ_l) + r_t * cos(θ_l) * tan(θ_l))) - θ_l + # epsilon = 1e-6 # Small value to prevent instability + # denominator = ( + # abs_depth_map * np.sin(latitudes_grid) + + # abs_depth_map * np.cos(latitudes_grid) * np.tan(latitudes_grid) + + # epsilon + # ) + # angular_disparity = np.arctan(self.baseline / denominator) - latitudes_grid + # + # self.disp_scale = 10 + # self.disp_offset = np.median(angular_disparity) + # + # # Normalize the disparity map for saving + # pixel_angle_scale = 1 # Assume scale factor is 1 if not provided (can be adjusted if needed) + # disparity_map = (angular_disparity / pixel_angle_scale - self.disp_offset) * self.disp_scale + + # # Normalize for visualization + # disparity_map_normalized = (disparity_map - disparity_map.min()) / (disparity_map.max() - disparity_map.min()) + # disparity_map_normalized *= 255 + # disparity_map_normalized = disparity_map_normalized.astype(np.uint8) + + # # Debugging visualization + # plt.imshow(disparity_map, cmap="jet") + # plt.colorbar() + # plt.title("Disparity Map (Debugging)") + # plt.show() + + # return disparity_map, disparity_map_normalized \ No newline at end of file diff --git a/scripts/simple_tab.py b/scripts/simple_tab.py index ab01fa7..026ab0d 100644 --- a/scripts/simple_tab.py +++ b/scripts/simple_tab.py @@ -172,16 +172,15 @@ class PipelineWorker(QThread): self.progress.emit("File saved in edgenet-360\Output") def run_pipeline(self): - #self.clean_temp_files() - #self.shift_image() - #self.copy_file() - #self.run_depth_estimation() - self.run_depth_to_disparity() - - #self.run_material_recognition() - #self.run_edge_net() - #self.run_post_processing() - self.progress.emit("Pipeline completed!") + self.clean_temp_files() + self.shift_image() + self.copy_file() + self.run_depth_estimation() + #self.run_depth_to_disparity() + self.run_material_recognition() + self.run_edge_net() + self.run_post_processing() + self.progress.emit("Pipeline completed!") class SimpleTab(QWidget): def __init__(self, config_reader): -- GitLab