Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Depth] Add normalize_points flag (#511)

tony.nga4e725e98d21
diff --git a/kornia/geometry/camera/perspective.py b/kornia/geometry/camera/perspective.py
index 7d1f6c627..e9083b6a1 100644
--- a/kornia/geometry/camera/perspective.py
+++ b/kornia/geometry/camera/perspective.py
@@ -75,7 +75,7 @@ def unproject_points(
points. The tensor shape must be equal to point2d :math:`(*, 1)`.
camera_matrix (torch.Tensor): tensor containing the intrinsics camera
matrix. The tensor shape must be Bx4x4.
- normalize (bool, optional): wether to normalize the pointcloud. This
+ normalize (bool, optional): whether to normalize the pointcloud. This
must be set to `True` when the depth is represented as the Euclidean
ray length from the camera position. Default is `False`.
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.py
index 33557eadf..528c600f1 100644
--- a/kornia/geometry/depth.py
+++ b/kornia/geometry/depth.py
@@ -10,12 +10,15 @@ from kornia.utils import create_meshgrid
from kornia.filters import spatial_gradient
-def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensor:
+def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor, normalize_points: bool = False) -> torch.Tensor:
"""Compute a 3d point per pixel given its depth value and the camera intrinsics.
Args:
depth (torch.Tensor): image tensor containing a depth value per pixel.
camera_matrix (torch.Tensor): tensor containing the camera intrinsics.
+ normalize_points (bool): whether to normalise the pointcloud. This
+ must be set to `True` when the depth is represented as the Euclidean
+ ray length from the camera position. Default is `False`.
Shape:
- Input: :math:`(B, 1, H, W)` and :math:`(B, 3, 3)`
@@ -51,17 +54,20 @@ def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tenso
# project pixels to camera frame
camera_matrix_tmp: torch.Tensor = camera_matrix[:, None, None] # Bx1x1x3x3
points_3d: torch.Tensor = unproject_points(
- points_2d, points_depth, camera_matrix_tmp, normalize=True) # BxHxWx3
+ points_2d, points_depth, camera_matrix_tmp, normalize=normalize_points) # BxHxWx3
return points_3d.permute(0, 3, 1, 2) # Bx3xHxW
-def depth_to_normals(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensor:
+def depth_to_normals(depth: torch.Tensor, camera_matrix: torch.Tensor, normalize_points: bool = False) -> torch.Tensor:
"""Compute the normal surface per pixel.
Args:
depth (torch.Tensor): image tensor containing a depth value per pixel.
camera_matrix (torch.Tensor): tensor containing the camera intrinsics.
+ normalize_points (bool): whether to normalise the pointcloud. This
+ must be set to `True` when the depth is represented as the Euclidean
+ ray length from the camera position. Default is `False`.
Shape:
- Input: :math:`(B, 1, H, W)` and :math:`(B, 3, 3)`
@@ -86,7 +92,7 @@ def depth_to_normals(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.
f"Got: {camera_matrix.shape}.")
# compute the 3d points from depth
- xyz: torch.Tensor = depth_to_3d(depth, camera_matrix) # Bx3xHxW
+ xyz: torch.Tensor = depth_to_3d(depth, camera_matrix, normalize_points) # Bx3xHxW
# compute the pointcloud spatial gradients
gradients: torch.Tensor = spatial_gradient(xyz) # Bx3x2xHxW
@@ -102,7 +108,8 @@ def warp_frame_depth(
image_src: torch.Tensor,
depth_dst: torch.Tensor,
src_trans_dst: torch.Tensor,
- camera_matrix: torch.Tensor) -> torch.Tensor:
+ camera_matrix: torch.Tensor,
+ normalize_points: bool = False) -> 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
@@ -113,6 +120,9 @@ def warp_frame_depth(
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).
+ normalize_points (bool): whether to normalise the pointcloud. This
+ must be set to `True` when the depth is represented as the Euclidean
+ ray length from the camera position. Default is `False`.
Return:
torch.Tensor: the warped tensor in the source frame with shape (Bx3xHxW).
@@ -146,7 +156,7 @@ def warp_frame_depth(
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
+ points_3d_dst: torch.Tensor = depth_to_3d(depth_dst, camera_matrix, normalize_points) # Bx3xHxW
# transform points from source to destionation
points_3d_dst = points_3d_dst.permute(0, 2, 3, 1) # BxHxWx3
diff --git a/test/geometry/test_depth.py b/test/geometry/test_depth.py
index d1bc5526b..62b495750 100644
--- a/test/geometry/test_depth.py
+++ b/test/geometry/test_depth.py
@@ -33,7 +33,43 @@ class TestDepthTo3d:
points3d = kornia.depth_to_3d(depth, camera_matrix)
assert points3d.shape == (batch_size, 3, 3, 4)
- def test_unproject(self, device):
+ def test_unproject_normalized(self, device):
+ # this is for default normalize_points=False
+ depth = 2 * torch.tensor([[[
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ ]]]).to(device)
+
+ camera_matrix = torch.tensor([[
+ [1., 0., 0.],
+ [0., 1., 0.],
+ [0., 0., 1.],
+ ]]).to(device)
+
+ points3d_expected = torch.tensor([[[
+ [0., 2., 4.],
+ [0., 2., 4.],
+ [0., 2., 4.],
+ [0., 2., 4.],
+ ], [
+ [0., 0., 0.],
+ [2., 2., 2.],
+ [4., 4., 4.],
+ [6., 6., 6.],
+ ], [
+ [2., 2., 2.],
+ [2., 2., 2.],
+ [2., 2., 2.],
+ [2., 2., 2.],
+ ]]]).to(device)
+
+ points3d = kornia.depth_to_3d(depth, camera_matrix) # default is normalize_points=False
+ assert_allclose(points3d, points3d_expected)
+
+ def test_unproject_normalized(self, device):
+ # this is for normalize_points=True
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
@@ -64,7 +100,7 @@ class TestDepthTo3d:
[0.6325, 0.6030, 0.5345],
]]]).to(device)
- points3d = kornia.depth_to_3d(depth, camera_matrix)
+ points3d = kornia.depth_to_3d(depth, camera_matrix, normalize_points=True)
assert_allclose(points3d, points3d_expected)
def test_unproject_and_project(self, device):
@@ -127,6 +163,42 @@ class TestDepthToNormals:
assert points3d.shape == (batch_size, 3, 3, 4)
def test_simple(self, device):
+ # this is for default normalize_points=False
+ depth = 2 * torch.tensor([[[
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ ]]]).to(device)
+
+ camera_matrix = torch.tensor([[
+ [1., 0., 0.],
+ [0., 1., 0.],
+ [0., 0., 1.],
+ ]]).to(device)
+
+ normals_expected = torch.tensor([[[
+ [0., 0., 0.],
+ [0., 0., 0.],
+ [0., 0., 0.],
+ [0., 0., 0.],
+ ], [
+ [0., 0., 0.],
+ [0., 0., 0.],
+ [0., 0., 0.],
+ [0., 0., 0.],
+ ], [
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ ]]]).to(device)
+
+ normals = kornia.depth_to_normals(depth, camera_matrix) # default is normalize_points=False
+ assert_allclose(normals, normals_expected, 1e-3, 1e-3)
+
+ def test_simple_normalized(self, device):
+ # this is for default normalize_points=False
depth = 2 * torch.tensor([[[
[1., 1., 1.],
[1., 1., 1.],
@@ -157,7 +229,7 @@ class TestDepthToNormals:
[0.4129, 0.4824, 0.4784],
]]]).to(device)
- normals = kornia.depth_to_normals(depth, camera_matrix)
+ normals = kornia.depth_to_normals(depth, camera_matrix, normalize_points=True)
assert_allclose(normals, normals_expected, 1e-3, 1e-3)
def test_gradcheck(self, device):
@@ -195,6 +267,48 @@ class TestWarpFrameDepth:
assert image_dst.shape == (batch_size, num_features, 3, 4)
def test_translation(self, device):
+ # this is for normalize_points=False
+ image_src = torch.tensor([[[
+ [1., 2., 3.],
+ [1., 2., 3.],
+ [1., 2., 3.],
+ [1., 2., 3.],
+ ]]]).to(device)
+
+ depth_dst = torch.tensor([[[
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ [1., 1., 1.],
+ ]]]).to(device)
+
+ src_trans_dst = torch.tensor([[
+ [1., 0., 0., 1.],
+ [0., 1., 0., 0.],
+ [0., 0., 1., 0.],
+ [0., 0., 0., 1.],
+ ]]).to(device)
+
+ h, w = image_src.shape[-2:]
+ camera_matrix = torch.tensor([[
+ [1., 0., w / 2],
+ [0., 1., h / 2],
+ [0., 0., 1.],
+ ]]).to(device)
+
+ image_dst_expected = torch.tensor([[[
+ [2., 3., 0.],
+ [2., 3., 0.],
+ [2., 3., 0.],
+ [2., 3., 0.],
+ ]]]).to(device)
+
+ image_dst = kornia.warp_frame_depth(
+ image_src, depth_dst, src_trans_dst, camera_matrix) # default is normalize_points=False
+ assert_allclose(image_dst, image_dst_expected, 1e-3, 1e-3)
+
+ def test_translation_normalized(self, device):
+ # this is for normalize_points=True
image_src = torch.tensor([[[
[1., 2., 3.],
[1., 2., 3.],
@@ -231,7 +345,7 @@ class TestWarpFrameDepth:
]]]).to(device)
image_dst = kornia.warp_frame_depth(
- image_src, depth_dst, src_trans_dst, camera_matrix)
+ image_src, depth_dst, src_trans_dst, camera_matrix, normalize_points=True)
assert_allclose(image_dst, image_dst_expected, 1e-3, 1e-3)
def test_gradcheck(self, device):