Overview
Provides operators for working with RGB-Depth images including depth-to-3D unprojection, surface normal estimation, depth warping between views, plane-based depth computation, and disparity-to-depth conversion.
Description
The depth.py module in the Kornia geometry library contains functions and an nn.Module class (DepthWarper) for depth-based geometric operations. Core functions include depth_to_3d and depth_to_3d_v2 for converting depth maps to 3D point clouds using camera intrinsics, depth_to_normals for computing per-pixel surface normals via spatial gradients and cross products, warp_frame_depth for warping images between viewpoints using depth maps and transformation matrices, depth_from_plane_equation for computing depth from plane normals and offsets, and depth_from_disparity for stereo depth recovery. The DepthWarper class provides a reusable nn.Module for depth-based image warping between pinhole camera views with sub-pixel accuracy.
Usage
Import these utilities when working with depth sensors, stereo vision, multi-view geometry, or any pipeline that needs to convert between 2D images and 3D geometry using depth information and camera parameters.
Code Reference
Source Location
Signature
def unproject_meshgrid(
height: int, width: int, camera_matrix: torch.Tensor,
normalize_points: bool = False,
device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None,
) -> torch.Tensor
def depth_to_3d_v2(
depth: torch.Tensor, camera_matrix: torch.Tensor,
normalize_points: bool = False,
xyz_grid: Optional[torch.Tensor] = None,
) -> torch.Tensor
def depth_to_3d(
depth: torch.Tensor, camera_matrix: torch.Tensor,
normalize_points: bool = False,
) -> torch.Tensor
def depth_to_normals(
depth: torch.Tensor, camera_matrix: torch.Tensor,
normalize_points: bool = False,
) -> torch.Tensor
def depth_from_plane_equation(
plane_normals: torch.Tensor, plane_offsets: torch.Tensor,
points_uv: torch.Tensor, camera_matrix: torch.Tensor,
eps: float = 1e-8,
) -> torch.Tensor
def warp_frame_depth(
image_src: torch.Tensor, depth_dst: torch.Tensor,
src_trans_dst: torch.Tensor, camera_matrix: torch.Tensor,
normalize_points: bool = False,
) -> torch.Tensor
class DepthWarper(nn.Module):
def __init__(
self, pinhole_dst: PinholeCamera, height: int, width: int,
mode: str = "bilinear", padding_mode: str = "zeros",
align_corners: bool = True,
) -> None
def compute_projection_matrix(self, pinhole_src: PinholeCamera) -> DepthWarper
def forward(self, depth_src: torch.Tensor, patch_dst: torch.Tensor) -> torch.Tensor
def depth_warp(
pinhole_dst: PinholeCamera, pinhole_src: PinholeCamera,
depth_src: torch.Tensor, patch_dst: torch.Tensor,
height: int, width: int, align_corners: bool = True,
) -> torch.Tensor
def depth_from_disparity(
disparity: torch.Tensor,
baseline: float | torch.Tensor,
focal: float | torch.Tensor,
) -> torch.Tensor
Import
from kornia.geometry.depth import (
depth_to_3d, depth_to_3d_v2, depth_to_normals,
warp_frame_depth, DepthWarper, depth_warp,
depth_from_disparity, depth_from_plane_equation,
unproject_meshgrid,
)
I/O Contract
Inputs (depth_to_3d)
| Name |
Type |
Required |
Description
|
| depth |
torch.Tensor |
Yes |
Depth map of shape (B, 1, H, W)
|
| camera_matrix |
torch.Tensor |
Yes |
Camera intrinsics of shape (B, 3, 3)
|
| normalize_points |
bool |
No |
Normalize the pointcloud for Euclidean ray length depth. Default: False
|
Outputs (depth_to_3d)
| Name |
Type |
Description
|
| points_3d |
torch.Tensor |
3D point cloud of shape (B, 3, H, W)
|
Inputs (depth_from_disparity)
| Name |
Type |
Required |
Description
|
| disparity |
torch.Tensor |
Yes |
Disparity tensor of shape (*, H, W)
|
| baseline |
float or torch.Tensor |
Yes |
Distance between stereo camera lenses
|
| focal |
float or torch.Tensor |
Yes |
Focal length of the camera
|
Outputs (depth_from_disparity)
| Name |
Type |
Description
|
| depth |
torch.Tensor |
Depth map of shape (*, H, W)
|
Usage Examples
import torch
from kornia.geometry.depth import depth_to_3d, depth_to_normals, depth_from_disparity
# Depth to 3D point cloud
depth = torch.rand(1, 1, 4, 4)
K = torch.eye(3).unsqueeze(0) # (1, 3, 3)
points_3d = depth_to_3d(depth, K)
# points_3d.shape: torch.Size([1, 3, 4, 4])
# Compute surface normals from depth
normals = depth_to_normals(depth, K)
# normals.shape: torch.Size([1, 3, 4, 4])
# Depth from stereo disparity
disparity = torch.rand(4, 1, 4, 4)
baseline = torch.rand(1)
focal = torch.rand(1)
depth_map = depth_from_disparity(disparity, baseline, focal)
# depth_map.shape: torch.Size([4, 1, 4, 4])
Related Pages