π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement warp_frame_depth
edgarribab1dbcb6f8f6e
diff --git a/docs/source/geometry.depth.rst b/docs/source/geometry.depth.rstindex d4542b73b..65574d5b2 100644--- a/docs/source/geometry.depth.rst+++ b/docs/source/geometry.depth.rst@@ -5,3 +5,4 @@ kornia.geometry.depth.. autofunction:: depth_to_3d.. autofunction:: depth_to_normals+.. autofunction:: warp_frame_depthdiff --git a/kornia/geometry/camera/perspective.py b/kornia/geometry/camera/perspective.pyindex a4e19282c..7d1f6c627 100644--- a/kornia/geometry/camera/perspective.py+++ b/kornia/geometry/camera/perspective.py@@ -43,20 +43,20 @@ def project_points(# project back using depth dividing in a safe wayxy_coords: torch.Tensor = convert_points_from_homogeneous(point_3d)- x_coord: torch.Tensor = xy_coords[..., 0:1]- y_coord: torch.Tensor = xy_coords[..., 1:2]+ x_coord: torch.Tensor = xy_coords[..., 0]+ y_coord: torch.Tensor = xy_coords[..., 1]# unpack intrinsics- fx: torch.Tensor = camera_matrix[..., 0:1, 0]- fy: torch.Tensor = camera_matrix[..., 1:2, 1]- cx: torch.Tensor = camera_matrix[..., 0:1, 2]- cy: torch.Tensor = camera_matrix[..., 1:2, 2]+ fx: torch.Tensor = camera_matrix[..., 0, 0]+ fy: torch.Tensor = camera_matrix[..., 1, 1]+ cx: torch.Tensor = camera_matrix[..., 0, 2]+ cy: torch.Tensor = camera_matrix[..., 1, 2]# apply intrinsics ans returnu_coord: torch.Tensor = x_coord * fx + cxv_coord: torch.Tensor = y_coord * fy + cy- return torch.cat([u_coord, v_coord], dim=-1)+ return torch.stack([u_coord, v_coord], dim=-1)def unproject_points(diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.pyindex c8c04d390..c6c629c41 100644--- a/kornia/geometry/depth.py+++ b/kornia/geometry/depth.py@@ -3,8 +3,10 @@import torchimport torch.nn.functional as F+from kornia.geometry import (+ project_points, unproject_points, transform_points, normalize_pixel_coordinates,+)from kornia.utils import create_meshgrid-from kornia.geometry import unproject_pointsfrom kornia.filters import spatial_gradient@@ -94,3 +96,75 @@ def depth_to_normals(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.normals: torch.Tensor = torch.cross(a, b, dim=1) # Bx3xHxWreturn F.normalize(normals, dim=1, p=2)+++def warp_frame_depth(+ image_src: torch.Tensor,+ depth_dst: torch.Tensor,+ src_trans_dst: torch.Tensor,+ camera_matrix: torch.Tensor) -> torch.Tensor:+ """Warp a tensor from a source to destination frame by the depth in the destination.++ Compute 3d points from the depth, transform them using given transformation, then project the point cloud to an+ image plane.++ Args:+ image_src (torch.Tensor): image tensor in the source frame with shape (BxDxHxW).+ depth_dst (torch.Tensor): depth tensor in the destination frame with shape (Bx1xHxW).+ src_trans_dst (torch.Tensor): transformation matrix from destination to source with shape (Bx4x4).+ camera_matrix (torch.Tensor): tensor containing the camera intrinsics with shape (Bx3x3).++ Return:+ torch.Tensor: the warped tensor in the source frame with shape (Bx3xHxW).++ """+ if not isinstance(image_src, torch.Tensor):+ raise TypeError(f"Input image_src type is not a torch.Tensor. Got {type(image_src)}.")++ if not len(image_src.shape) == 4:+ raise ValueError(f"Input image_src musth have a shape (B, D, H, W). Got: {image_src.shape}")++ if not isinstance(depth_dst, torch.Tensor):+ raise TypeError(f"Input depht_dst type is not a torch.Tensor. Got {type(depth_dst)}.")++ if not len(depth_dst.shape) == 4 and depth_dst.shape[-3] == 1:+ raise ValueError(f"Input depth_dst musth have a shape (B, 1, H, W). Got: {depth_dst.shape}")++ if not isinstance(src_trans_dst, torch.Tensor):+ raise TypeError(f"Input src_trans_dst type is not a torch.Tensor. "+ f"Got {type(src_trans_dst)}.")++ if not len(src_trans_dst.shape) == 3 and src_trans_dst.shape[-2:] == (3, 3):+ raise ValueError(f"Input src_trans_dst must have a shape (B, 3, 3). "+ f"Got: {src_trans_dst.shape}.")++ if not isinstance(camera_matrix, torch.Tensor):+ raise TypeError(f"Input camera_matrix type is not a torch.Tensor. "+ f"Got {type(camera_matrix)}.")++ if not len(camera_matrix.shape) == 3 and camera_matrix.shape[-2:] == (3, 3):+ raise ValueError(f"Input camera_matrix must have a shape (B, 3, 3). "+ f"Got: {camera_matrix.shape}.")+ # unproject source points to camera frame+ points_3d_dst: torch.Tensor = depth_to_3d(depth_dst, camera_matrix) # Bx3xHxW++ # transform points from source to destionation+ points_3d_dst = points_3d_dst.permute(0, 2, 3, 1) # BxHxWx3++ # hack to match sizes+ batch_size: int = image_src.shape[0]+ points_3d_dst = points_3d_dst.expand(batch_size, -1, -1, -1)+ src_trans_dst = src_trans_dst[:, None]++ points_3d_src = transform_points(src_trans_dst, points_3d_dst) # BxHxWx3++ # project back to pixels+ camera_matrix_tmp: torch.Tensor = camera_matrix[:, None, None] # Bx1x1xHxW+ points_2d_src: torch.Tensor = project_points(points_3d_src, camera_matrix_tmp) # BxHxWx2++ # normalize points between [-1 / 1]+ height, width = depth_dst.shape[-2:]+ points_2d_src_norm: torch.Tensor = normalize_pixel_coordinates(+ points_2d_src, height, width) # BxHxWx2++ return F.grid_sample(image_src, points_2d_src_norm) # BxDxHxWdiff --git a/test/geometry/test_depth.py b/test/geometry/test_depth.pyindex 28acd8b03..8a1dc745b 100644--- a/test/geometry/test_depth.py+++ b/test/geometry/test_depth.py@@ -62,7 +62,7 @@ class TestDepthToNormals:points3d = kornia.depth_to_normals(depth, camera_matrix)assert points3d.shape == (batch_size, 3, 3, 4)- @pytest.mark.parametrize("batch_size", [1, 2, 4, 5])+ @pytest.mark.parametrize("batch_size", [2, 4, 5])def test_shapes_broadcast(self, batch_size):depth = torch.rand(batch_size, 1, 3, 4)camera_matrix = torch.rand(1, 3, 3)@@ -81,3 +81,42 @@ class TestDepthToNormals:# evaluate function gradientassert gradcheck(kornia.depth_to_normals, (depth, camera_matrix,),raise_exception=True)+++class TestWarpFrameDepth:+ def test_smoke(self):+ image_src = torch.rand(1, 3, 3, 4)+ depth_dst = torch.rand(1, 1, 3, 4)+ src_trans_dst = torch.rand(1, 4, 4)+ camera_matrix = torch.rand(1, 3, 3)++ image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)+ assert image_dst.shape == (1, 3, 3, 4)++ @pytest.mark.parametrize("batch_size", [2, 4, 5])+ @pytest.mark.parametrize("num_features", [1, 3, 5])+ def test_shape(self, batch_size, num_features):+ image_src = torch.rand(batch_size, num_features, 3, 4)+ depth_dst = torch.rand(batch_size, 1, 3, 4)+ src_trans_dst = torch.rand(batch_size, 4, 4)+ camera_matrix = torch.rand(batch_size, 3, 3)++ image_dst = kornia.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)+ assert image_dst.shape == (batch_size, num_features, 3, 4)++ def test_gradcheck(self):+ image_src = torch.rand(1, 3, 3, 4)+ image_src = utils.tensor_to_gradcheck_var(image_src) # to var++ depth_dst = torch.rand(1, 1, 3, 4)+ depth_dst = utils.tensor_to_gradcheck_var(depth_dst) # to var++ src_trans_dst = torch.rand(1, 4, 4)+ src_trans_dst = utils.tensor_to_gradcheck_var(src_trans_dst) # to var++ camera_matrix = torch.rand(1, 3, 3)+ camera_matrix = utils.tensor_to_gradcheck_var(camera_matrix) # to var++ # evaluate function gradient+ assert gradcheck(kornia.warp_frame_depth, (image_src, depth_dst, src_trans_dst, camera_matrix,),+ raise_exception=True)