Skip to content

Commit

Permalink
fix: Update gemoetry.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AtticusZeller authored Jun 10, 2024
1 parent a4e2f70 commit 20a8122
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/pose_estimation/gemoetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,22 @@ def unproject_depth(pcd: Tensor, pose: Tensor, intrinsics: Tensor) -> Tensor:
torch.Tensor
The depth image created from the point cloud with dimensions [height, width].
"""
pcd_camera = torch.einsum("hwj,jk->hwk", pcd, torch.inverse(pose))

x = pcd_camera[..., 0]
y = pcd_camera[..., 1]
z = pcd_camera[..., 2]
u = (x / z) * intrinsics[0, 0] + intrinsics[0, 2]
v = (y / z) * intrinsics[1, 1] + intrinsics[1, 2]

grid = torch.stack((u / z.shape[1], v / z.shape[0]), dim=-1) * 2 - 1 # Normalize to [-1, 1]
depth = z.unsqueeze(0).unsqueeze(1)

projected_depth = F.grid_sample(depth, grid, mode='bilinear', padding_mode='zeros', align_corners=False)
return projected_depth.squeeze()
# Inverse transform to world coordinates (assuming pcd includes homogenous coordinate)
homogenous_world = torch.einsum('hwj,jk->hwk', pcd[..., 2:], torch.inverse(pose))

# Convert homogenous coordinates back to Euclidean coordinates in the camera frame
xyz_camera = homogenous_world[..., :3] / (homogenous_world[..., 3:4] + 1e-10) # Adding epsilon to avoid division by zero

# Project points using intrinsics
projected = torch.einsum('ij,hwj->hwi', intrinsics, xyz_camera)
u = projected[:, :, 0] / (projected[:, :, 2] + 1e-10) # u coordinate
v = projected[:, :, 1] / (projected[:, :, 2] + 1e-10) # v coordinate

# Map (u, v) back to depth values
# Since depth values are typically in z, directly extract z from the transformed coordinates
depth_image = xyz_camera[:, :, 2]

return depth_image


def compute_silhouette_diff(depth: Tensor, rastered_depth: Tensor) -> Tensor:
Expand Down

0 comments on commit 20a8122

Please sign in to comment.