Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(camera): reject an unproject_points_orthographic extension whose trailing size is not 1 (#6470)

sarankumar2550b0daa788
diff --git a/changelog.d/6470.fixed.md b/changelog.d/6470.fixed.md
new file mode 100644
index 000000000..bd55f26ab
--- /dev/null
+++ b/changelog.d/6470.fixed.md
@@ -0,0 +1 @@
+`unproject_points_orthographic` checks that `extension` has trailing size 1, as `unproject_points_z1` does. An extension with the same rank as the points and a trailing size other than 1 used to be concatenated whole, so the result had more than three coordinates; it now raises `ShapeError`.
diff --git a/kornia/geometry/camera/projection_orthographic.py b/kornia/geometry/camera/projection_orthographic.py
index eb7f5bf01..5cb0f3bb9 100644
--- a/kornia/geometry/camera/projection_orthographic.py
+++ b/kornia/geometry/camera/projection_orthographic.py
@@ -74,8 +74,9 @@ def unproject_points_orthographic(points_in_camera: torch.Tensor, extension: tor
"""
KORNIA_CHECK_SHAPE(points_in_camera, ["*", "2"])
- if len(points_in_camera.shape) != len(extension.shape):
- extension = extension[..., None]
+ if extension.ndim == points_in_camera.ndim - 1:
+ extension = extension[..., None] # (..., 1)
+ KORNIA_CHECK_SHAPE(extension, ["*", "1"])
return torch.cat([points_in_camera, extension], dim=-1)
diff --git a/tests/geometry/camera/test_projections.py b/tests/geometry/camera/test_projections.py
index 30257bf1d..6dc8ed259 100644
--- a/tests/geometry/camera/test_projections.py
+++ b/tests/geometry/camera/test_projections.py
@@ -291,6 +291,20 @@ class TestProjectionOrthographic(BaseTester):
with pytest.raises(ShapeError):
unproject_points_orthographic(points, extension)
+ def test_exception_extension_trailing_size_not_1(self, device, dtype) -> None:
+ # The extension is the z column, (..., 1) or (...,): one of the same rank with a trailing size other
+ # than 1 used to be concatenated whole and returned more than three coordinates.
+ from kornia.core.exceptions import ShapeError
+
+ points = torch.ones(5, 2, device=device, dtype=dtype)
+ for extension in (torch.ones(5, 2, device=device, dtype=dtype), torch.ones(5, 3, device=device, dtype=dtype)):
+ with pytest.raises(ShapeError):
+ unproject_points_orthographic(points, extension)
+ with pytest.raises(ShapeError):
+ unproject_points_orthographic(
+ torch.ones(2, device=device, dtype=dtype), torch.ones(2, device=device, dtype=dtype)
+ )
+
def test_convention_dx_orthographic_is_the_scalar_du_dx_not_the_full_jacobian(self, device, dtype):
# dx_project_points_orthographic returns the scalar du/dx = 1 with shape (..., 1), not the (2, 3)
# Jacobian that its z1 sibling dx_project_points_z1 returns. Every literal is dyadic, so exact.