Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(camera)!: build the pinhole projection from the intrinsics 3x3 block (#5415)

niukanen1378535780020
diff --git a/changelog.d/5415.breaking.md b/changelog.d/5415.breaking.md
new file mode 100644
index 000000000..46ea792f2
--- /dev/null
+++ b/changelog.d/5415.breaking.md
@@ -0,0 +1,8 @@
+:class:`~kornia.geometry.camera.pinhole.PinholeCamera` ``project`` and ``unproject`` now build their projection
+matrix from the top-left 3x3 block of ``intrinsics`` only (#4771). A 3x3 ``K`` zero-padded to 4x4
+(``intrinsics[3, 3] = 0``) used to project while ``unproject`` raised ``LinAlgError`` on the singular 4x4 product;
+it now round-trips exactly like its homogeneous embedding. The other entries of the 4x4 no longer enter either
+method: before, a non-zero ``intrinsics[0, 3]`` shifted every projected ``u`` by ``intrinsics[0, 3] / z`` (with
+``K = [[100, 0, 4], [0, 100, 3], [0, 0, 1]]`` and ``intrinsics[0, 3] = 10``, the point ``(1, 2, 4)`` projected to
+``u = 31.5``, now ``29``), and ``intrinsics[3, 3] = 2`` doubled every point ``unproject`` returned. Intrinsics in
+the documented homogeneous form give the same results as before, bit for bit on CPU.
diff --git a/docs/source/get-started/camera-conventions.rst b/docs/source/get-started/camera-conventions.rst
index a540925db..66f78f332 100644
--- a/docs/source/get-started/camera-conventions.rst
+++ b/docs/source/get-started/camera-conventions.rst
@@ -180,12 +180,11 @@ Intrinsics layout
:func:`kornia.geometry.camera.perspective.project_points` alike. With a skewed ``K``, apply ``inv(K)`` to homogeneous
pixel coordinates, or ``K`` to normalized ones, directly.
- :class:`kornia.geometry.camera.pinhole.PinholeCamera` instead stores a ``4x4`` ``intrinsics`` whose canonical
- form is the homogeneous embedding ``[[fx, 0, cx, 0], [0, fy, cy, 0], [0, 0, 1, 0], [0, 0, 0, 1]]``. The whole
- matrix participates: :meth:`~kornia.geometry.camera.pinhole.PinholeCamera.project` multiplies the full
- ``intrinsics @ extrinsics`` and :meth:`~kornia.geometry.camera.pinhole.PinholeCamera.unproject` inverts that
- ``4x4`` product, so a non-zero ``intrinsics[0, 3]`` shifts every projected ``u`` by ``intrinsics[0, 3] / z``
- and ``intrinsics[3, 3]`` rescales every unprojected point. The layout is not validated: a ``3x3`` ``K`` zero-padded
- without ``intrinsics[3, 3] = 1`` still projects, but ``unproject`` fails on a singular matrix
+ form is the homogeneous embedding ``[[fx, 0, cx, 0], [0, fy, cy, 0], [0, 0, 1, 0], [0, 0, 0, 1]]``.
+ :meth:`~kornia.geometry.camera.pinhole.PinholeCamera.project` and
+ :meth:`~kornia.geometry.camera.pinhole.PinholeCamera.unproject` build their projection from the top-left
+ ``3x3`` block only, so a non-zero ``intrinsics[0, 3]`` never enters a projection, and a ``3x3`` ``K``
+ zero-padded without ``intrinsics[3, 3] = 1`` round-trips exactly like its homogeneous embedding
(`#4771 <https://github.com/kornia/kornia/issues/4771>`_). Its class docstring documents the layout and the
zero-padded case.
- **Depth means two different things.** It is the camera-frame ``z`` by default, and the Euclidean ray length
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.py
index 6609ed276..5569dc730 100644
--- a/kornia/geometry/camera/pinhole.py
+++ b/kornia/geometry/camera/pinhole.py
@@ -50,9 +50,9 @@ class PinholeCamera:
which disagrees with the integer pixel centres above:
`#4263 <https://github.com/kornia/kornia/issues/4263>`_. With the :math:`(B, N, 4, 4)` storage the
validator admits, :meth:`project` raises on :math:`(B, N, 3)` points:
- `#4266 <https://github.com/kornia/kornia/issues/4266>`_. The ``intrinsics`` form is not validated: a
- zero-padded ``K`` with ``intrinsics[3, 3] = 0`` projects but :meth:`unproject` raises on the singular
- matrix: `#4771 <https://github.com/kornia/kornia/issues/4771>`_.
+ `#4266 <https://github.com/kornia/kornia/issues/4266>`_. The ``intrinsics`` form is not validated, but
+ :meth:`project` and :meth:`unproject` read only the top-left :math:`3 \times 3` block, so a zero-padded
+ ``K`` round-trips like its homogeneous embedding.
Args:
intrinsics: torch.Tensor with shape :math:`(B, 4, 4)`
@@ -398,7 +398,7 @@ class PinholeCamera:
"""
if len(point_3d.shape) < 2:
raise ValueError(f"Input must be at least a 2D tensor. Got {point_3d.shape}")
- P = self.intrinsics @ self.extrinsics
+ P = self._projection_matrix()
return convert_points_from_homogeneous(transform_points(P, point_3d))
def unproject(self, point_2d: torch.Tensor, depth: torch.Tensor) -> torch.Tensor:
@@ -431,10 +431,26 @@ class PinholeCamera:
tensor([[0.4963, 0.7682, 1.0000]])
"""
- P = self.intrinsics @ self.extrinsics
- P_inv = _torch_inverse_cast(P)
+ P_inv = _torch_inverse_cast(self._projection_matrix())
return transform_points(P_inv, convert_points_to_homogeneous(point_2d) * depth)
+ def _projection_matrix(self) -> torch.Tensor:
+ r"""Return the 4x4 projection built from the intrinsics' top-left 3x3 block.
+
+ Both :meth:`project` and :meth:`unproject` go through this matrix, so they agree on
+ which intrinsics are usable: a 3x3 ``K`` zero-padded to 4x4 round-trips exactly like its
+ homogeneous embedding, instead of projecting while ``unproject`` raises on the singular
+ 4x4 product. For a homogeneous embedding the result matches ``intrinsics @ extrinsics``
+ bit for bit on CPU, and to the dtype's rounding on MPS.
+ """
+ K = self._intrinsics[..., :3, :3]
+ E = self._extrinsics
+ P = torch.zeros(*K.shape[:-2], 4, 4, device=K.device, dtype=K.dtype)
+ P[..., :3, :3] = K @ E[..., :3, :3]
+ P[..., :3, 3:] = K @ E[..., :3, 3:]
+ P[..., 3, 3] = 1.0
+ return P
+
# NOTE: just for test. Decide if we keep it.
@classmethod
def from_parameters(
diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.py
index 2c0121225..65596ca08 100644
--- a/tests/geometry/camera/test_pinhole.py
+++ b/tests/geometry/camera/test_pinhole.py
@@ -948,11 +948,10 @@ class TestPinholeCamera(BaseTester):
regular = torch.tensor([[1.0, 2.0, 4.0]], device=device, dtype=dtype)
self.assert_close(cam.project(regular), kornia.geometry.camera.project_points(regular, K3))
- def test_wart_unproject_fails_on_zero_padded_intrinsics_4771(self, device, dtype):
- # Wart pin for #4771: a 3x3 K zero-padded to 4x4 ([3, 3] = 0) constructs and projects correctly, but
- # unproject inverts the singular intrinsics @ extrinsics and raises. Delete or invert when #4771 is
- # repaired (constructor validation or a 3x3-block unproject both fail this pin). The control sets
- # [3, 3] = 1 and changes nothing else, so the raise is attributable to the zero pad.
+ def test_convention_project_and_unproject_read_the_3x3_block_4771(self, device, dtype):
+ # Convention pin for #4771: project and unproject both build the projection from the intrinsics'
+ # top-left 3x3 block, so a 3x3 K zero-padded to 4x4 ([3, 3] = 0) round-trips exactly like its
+ # homogeneous embedding. The control sets [3, 3] = 1 and changes nothing else.
height, width = torch.tensor([6], device=device), torch.tensor([8], device=device)
point = torch.tensor([[1.0, 2.0, 4.0]], device=device, dtype=dtype)
depth = torch.tensor([[4.0]], device=device, dtype=dtype)
@@ -967,8 +966,16 @@ class TestPinholeCamera(BaseTester):
kornia.geometry.camera.PinholeCamera(control, _e44(device, dtype), height, width).unproject(uv, depth),
point,
)
- with pytest.raises(RuntimeError):
- cam.unproject(uv, depth)
+ self.assert_close(cam.unproject(uv, depth), point)
+
+ # The rest of the 4x4 is ignored too. Before #4771 was fixed, intrinsics[0, 3] = 10 shifted u by
+ # 10 / z (29 -> 31.5) and intrinsics[3, 3] = 2 doubled every unprojected point.
+ shifted = control.clone()
+ shifted[:, 0, 3] = 10.0
+ shifted[:, 3, 3] = 2.0
+ shifted_cam = kornia.geometry.camera.PinholeCamera(shifted, _e44(device, dtype), height, width)
+ self.assert_close(shifted_cam.project(point), uv)
+ self.assert_close(shifted_cam.unproject(uv, depth), point)
def test_convention_from_parameters_fills_every_batch_element_4279(self, device, dtype):
# Regression pin for #4279: image size must be filled for every camera in the batch.