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

Real depth working, disparity not currently feasible

parent c1cc588d
Branches
No related tags found
1 merge request!19Gdp 4.5.1 implement disparity
......@@ -109,62 +109,103 @@ 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):
"""
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.
# Get image dimensions
# Get image dimensions
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
# Calculate step sizes
unit_w = 2.0 / width # Longitude step size
unit_h = 1.0 / height # Latitude step size
# 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))
# Initialize disparity map
disparity_map = np.zeros_like(abs_depth_map, dtype=np.float32)
# 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
# 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]
# 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
# Avoid invalid or infinite depth values
if r_t <= 0:
disparity_map[i, j] = 0
continue
self.disp_scale = 10
self.disp_offset = np.median(angular_disparity)
try:
# Compute denominator for the formula
# 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
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
# 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
if denominator <= 0:
disparity_map[i, j] = 0
continue
denominator[denominator <= 0] = epsilon
# Calculate angular disparity
angle_disp = np.arctan(self.baseline / denominator) - theta_t
angle_disp = np.arctan(self.baseline / denominator) - theta_t_grid
# 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
disparity_map = (angle_disp / (unit_h * np.pi) - self.disp_offset) * self.disp_scale
plt.imshow(disparity_map, cmap='gray')
plt.colorbar()
......@@ -178,4 +219,3 @@ class Depth2Disparity:
return disparity_map, disparity_map_normalized
\ No newline at end of file
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment