Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement depth_to_normals

edgarriba17545417c499
diff --git a/docs/source/geometry.depth.rst b/docs/source/geometry.depth.rst
index ef71fb4f7..d4542b73b 100644
--- a/docs/source/geometry.depth.rst
+++ b/docs/source/geometry.depth.rst
@@ -3,5 +3,5 @@ kornia.geometry.depth
.. currentmodule:: kornia.geometry.depth
-
.. autofunction:: depth_to_3d
+.. autofunction:: depth_to_normals
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.py
index b7347e316..c8c04d390 100644
--- a/kornia/geometry/depth.py
+++ b/kornia/geometry/depth.py
@@ -5,6 +5,7 @@ import torch.nn.functional as F
from kornia.utils import create_meshgrid
from kornia.geometry import unproject_points
+from kornia.filters import spatial_gradient
def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tensor:
@@ -51,3 +52,45 @@ def depth_to_3d(depth: torch.Tensor, camera_matrix: torch.Tensor) -> torch.Tenso
points_2d, points_depth, camera_matrix_tmp, normalize=True) # BxHxWx3
return points_3d.permute(0, 3, 1, 2) # Bx3xHxW
+
+
+def depth_to_normals(depth: torch.Tensor, camera_matrix: torch.Tensor) -> 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.
+
+ Shape:
+ - Input: :math:`(B, 1, H, W)` and :math:`(B, 3, 3)`
+ - Output: :math:`(B, 3, H, W)`
+
+ Return:
+ torch.Tensor: tensor with a normal surface vector per pixel of the same resolution as the input.
+
+ """
+ if not isinstance(depth, torch.Tensor):
+ raise TypeError(f"Input depht type is not a torch.Tensor. Got {type(depth)}.")
+
+ if not len(depth.shape) == 4 and depth.shape[-3] == 1:
+ raise ValueError(f"Input depth musth have a shape (B, 1, H, W). Got: {depth.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}.")
+
+ # compute the 3d points from depth
+ xyz: torch.Tensor = depth_to_3d(depth, camera_matrix) # Bx3xHxW
+
+ # compute the pointcloud spatial gradients
+ gradients: torch.Tensor = spatial_gradient(xyz) # Bx3x2xHxW
+
+ # compute normals
+ a, b = gradients[:, :, 0], gradients[:, :, 1] # Bx3xHxW
+
+ normals: torch.Tensor = torch.cross(a, b, dim=1) # Bx3xHxW
+ return F.normalize(normals, dim=1, p=2)
diff --git a/test/geometry/test_depth.py b/test/geometry/test_depth.py
index 28771a96d..28acd8b03 100644
--- a/test/geometry/test_depth.py
+++ b/test/geometry/test_depth.py
@@ -44,3 +44,40 @@ class TestDepthTo3D:
# evaluate function gradient
assert gradcheck(kornia.depth_to_3d, (depth, camera_matrix,),
raise_exception=True)
+
+
+class TestDepthToNormals:
+ def test_smoke(self):
+ depth = torch.rand(1, 1, 3, 4)
+ camera_matrix = torch.rand(1, 3, 3)
+
+ points3d = kornia.depth_to_normals(depth, camera_matrix)
+ assert points3d.shape == (1, 3, 3, 4)
+
+ @pytest.mark.parametrize("batch_size", [2, 4, 5])
+ def test_shapes(self, batch_size):
+ depth = torch.rand(batch_size, 1, 3, 4)
+ camera_matrix = torch.rand(batch_size, 3, 3)
+
+ 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])
+ def test_shapes_broadcast(self, batch_size):
+ depth = torch.rand(batch_size, 1, 3, 4)
+ camera_matrix = torch.rand(1, 3, 3)
+
+ points3d = kornia.depth_to_normals(depth, camera_matrix)
+ assert points3d.shape == (batch_size, 3, 3, 4)
+
+ def test_gradcheck(self):
+ # generate input data
+ depth = torch.rand(1, 1, 3, 4)
+ depth = utils.tensor_to_gradcheck_var(depth) # 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.depth_to_normals, (depth, camera_matrix,),
+ raise_exception=True)