diff --git a/scripts/Depth2Disparity/depth_to_disparity.py b/scripts/Depth2Disparity/depth_to_disparity.py index d13d3dc369c7f39d170123eedc2370a53092e195..10c0df9b5972fba4d15013290d3a3bdbad7beaca 100644 --- a/scripts/Depth2Disparity/depth_to_disparity.py +++ b/scripts/Depth2Disparity/depth_to_disparity.py @@ -109,73 +109,113 @@ class Depth2Disparity: abs_depth_map = a * rel_depth_map + b print("Applied transformation to entire depth map") - abs_depth_map = np.maximum(abs_depth_map, 0.01) + #abs_depth_map = np.maximum(abs_depth_map, 0.01) - #plt.imshow(abs_depth_map, cmap='gray') - #plt.colorbar() - #plt.show() + plt.imshow(abs_depth_map, cmap='gray') + plt.colorbar() + plt.show() # this should not be normalised, the values in the array should equate to literal distances return abs_depth_map def _depth2disparity(self, abs_depth_map): - - # Get image dimensions - # Get image dimensions - height, width = abs_depth_map.shape - - # Calculate step sizes - unit_w = 2.0 / width # Longitude step size - unit_h = 1.0 / height # Latitude step size - - # Initialize disparity map - disparity_map = np.zeros_like(abs_depth_map, dtype=np.float32) - - # Iterate over each pixel - for i in range(height): - # Calculate latitude angle for the row - theta_t = i * unit_h * np.pi - - for j in range(width): - # Retrieve the absolute depth (r_t) for this pixel - r_t = abs_depth_map[i, j] - - # Avoid invalid or infinite depth values - if r_t <= 0: - disparity_map[i, j] = 0 - continue - - try: - # Compute denominator for the formula - epsilon = 1e-8 # Small value to prevent 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 - - # Avoid invalid denominator values - if denominator <= 0: - disparity_map[i, j] = 0 - continue - - # Calculate angular disparity - angle_disp = np.arctan(self.baseline / denominator) - theta_t - - # Convert angular disparity to pixel disparity - 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 - - 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 + """ + 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 + + 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