Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Implement `project` and `unproject` in `PinholeCamera` (#1729)

Yaniv Hollander496e13de21f3
diff --git a/.gitignore b/.gitignore
index 3ecc12661..8498662d8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,3 +19,4 @@ kornia.egg-info/
*bak
*ipynb_checkpoints*
docs/source/_static/*jpg
+*DS_Store
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.py
index 1913cbb2e..6f312144b 100644
--- a/kornia/geometry/camera/pinhole.py
+++ b/kornia/geometry/camera/pinhole.py
@@ -2,7 +2,9 @@ from typing import Iterable, Optional
import torch
+from kornia.geometry.conversions import convert_points_from_homogeneous, convert_points_to_homogeneous
from kornia.geometry.linalg import inverse_transformation, transform_points
+from kornia.utils.helpers import _torch_inverse_cast
class PinholeCamera:
@@ -266,6 +268,63 @@ class PinholeCamera:
self.width *= scale_factor
return self
+ def project(self, point_3d: torch.Tensor) -> torch.Tensor:
+ r"""Project a 3d point in world coordinates onto the 2d camera plane.
+
+ Args:
+ point3d: tensor containing the 3d points to be projected
+ to the camera plane. The shape of the tensor can be :math:`(*, 3)`.
+
+ Returns:
+ tensor of (u, v) cam coordinates with shape :math:`(*, 2)`.
+
+ Example:
+ >>> _ = torch.manual_seed(0)
+ >>> X = torch.rand(1, 3)
+ >>> I = torch.eye(4)[None]
+ >>> E = torch.eye(4)[None]
+ >>> h = torch.ones(1)
+ >>> w = torch.ones(1)
+ >>> pinhole = kornia.geometry.camera.PinholeCamera(K, E, h, w)
+ >>> pinhole.project_points(X)
+ tensor([[5.6088, 8.6827]])
+ """
+ P = self.intrinsics @ self.extrinsics
+ return convert_points_from_homogeneous(transform_points(P, point_3d))
+
+ def unproject(self, point_2d: torch.Tensor, depth: torch.Tensor):
+ r"""Unproject a 2d point in 3d.
+
+ Transform coordinates in the pixel frame to the world frame.
+
+ Args:
+ point2d: tensor containing the 2d to be projected to
+ world coordinates. The shape of the tensor can be :math:`(*, 2)`.
+ depth: tensor containing the depth value of each 2d
+ points. The tensor shape must be equal to point2d :math:`(*, 1)`.
+ normalize: 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.
+
+ Returns:
+ tensor of (x, y, z) world coordinates with shape :math:`(*, 3)`.
+
+ Example:
+ >>> _ = torch.manual_seed(0)
+ >>> x = torch.rand(1, 2)
+ >>> depth = torch.ones(1, 1)
+ >>> I = torch.eye(4)[None]
+ >>> E = torch.eye(4)[None]
+ >>> h = torch.ones(1)
+ >>> w = torch.ones(1)
+ >>> pinhole = kornia.geometry.camera.PinholeCamera(K, E, h, w)
+ >>> pinhole.unproject_points(x, depth)
+ tensor([[0.4963, 0.7682, 1.0000]])
+ """
+ P = self.intrinsics @ self.extrinsics
+ P_inv = _torch_inverse_cast(P)
+ return transform_points(P_inv, convert_points_to_homogeneous(point_2d) * depth)
+
# NOTE: just for test. Decide if we keep it.
@classmethod
def from_parameters(
diff --git a/test/geometry/camera/test_pinhole.py b/test/geometry/camera/test_pinhole.py
index 8412095d8..7c7838eea 100644
--- a/test/geometry/camera/test_pinhole.py
+++ b/test/geometry/camera/test_pinhole.py
@@ -1,3 +1,5 @@
+import math
+
import pytest
import torch
from torch.autograd import gradcheck
@@ -240,6 +242,36 @@ class TestPinholeCamera:
extrinsics[..., 2, -1] = tz
return extrinsics.expand(batch_size, -1, -1)
+ def _create_extrinsics_with_rotation(self, batch_size, alpha, beta, gamma, tx, ty, tz, device, dtype):
+ Rx = torch.eye(3, device=device, dtype=dtype)
+ Rx[1, 1] = math.cos(alpha)
+ Rx[1, 2] = math.sin(alpha)
+ Rx[2, 1] = -Rx[1, 2]
+ Rx[2, 2] = Rx[1, 1]
+
+ Ry = torch.eye(3, device=device, dtype=dtype)
+ Ry[0, 0] = math.cos(beta)
+ Ry[0, 2] = -math.sin(beta)
+ Ry[2, 0] = -Ry[0, 2]
+ Ry[2, 2] = Ry[0, 0]
+
+ Rz = torch.eye(3, device=device, dtype=dtype)
+ Rz[0, 0] = math.cos(gamma)
+ Rz[0, 1] = math.sin(gamma)
+ Rz[1, 0] = -Rz[0, 1]
+ Rz[1, 1] = Rz[0, 0]
+
+ Ryz = torch.matmul(Ry, Rz)
+ R = torch.matmul(Rx, Ryz)
+
+ extrinsics = torch.eye(4, device=device, dtype=dtype)
+ extrinsics[..., 0, -1] = tx
+ extrinsics[..., 1, -1] = ty
+ extrinsics[..., 2, -1] = tz
+ extrinsics[:3, :3] = R
+
+ return extrinsics.expand(batch_size, -1, -1)
+
def test_smoke(self, device, dtype):
intrinsics = torch.eye(4, device=device, dtype=dtype)[None]
extrinsics = torch.eye(4, device=device, dtype=dtype)[None]
@@ -401,3 +433,29 @@ class TestPinholeCamera:
) # cy
assert_close(pinhole_scale.height, pinhole.height * scale_val, atol=1e-4, rtol=1e-4)
assert_close(pinhole_scale.width, pinhole.width * scale_val, atol=1e-4, rtol=1e-4)
+
+ def test_pinhole_camera_project_and_unproject(self, device, dtype):
+ batch_size = 5
+ n = 2 # Point per batch
+ height, width = 4, 6
+ fx, fy, cx, cy = 1, 2, width / 2, height / 2
+ alpha, beta, gamma = 0.0, 0.0, 0.4
+ tx, ty, tz = 0, 0, 3
+
+ intrinsics = self._create_intrinsics(batch_size, fx, fy, cx, cy, device=device, dtype=dtype)
+ extrinsics = self._create_extrinsics_with_rotation(
+ batch_size, alpha, beta, gamma, tx, ty, tz, device=device, dtype=dtype
+ )
+
+ height = torch.ones(batch_size, device=device, dtype=dtype) * height
+ width = torch.ones(batch_size, device=device, dtype=dtype) * width
+
+ pinhole = kornia.geometry.camera.PinholeCamera(intrinsics, extrinsics, height, width)
+
+ point_3d = torch.rand((batch_size, n, 3), device=device, dtype=dtype)
+
+ depth = point_3d[..., -1:] + tz
+
+ point_2d = pinhole.project(point_3d)
+ point_3d_hat = pinhole.unproject(point_2d, depth)
+ assert_close(point_3d, point_3d_hat, atol=1e-4, rtol=1e-4)