Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): validate pixel2cam depth shape before transformation (#4314)

Kevin3c74d42e0e1c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e0c55464..38db3dba3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -446,6 +446,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
drops the failure count to 1,269 β€” a real, separate residual (a disconnected-graph case with
two simultaneous double-root pairs) remains and is tracked in #4290, not fixed here.
+* `pixel2cam` now rejects depth tensors outside the documented `Bx1xHxW` shape. The guard's predicate
+ bound as `(ndim != 4) and (shape[1] == 1)`, so four-dimensional multi-channel depth bypassed the
+ channel check β€” a three-channel depth silently broadcast into separate camera coordinates β€” while
+ scalar and one-dimensional inputs raised `IndexError` from the guard itself. Code that passed those
+ shapes and relied on them being accepted now raises `ValueError`. Valid `Bx1xHxW` depth is unaffected
+ and its output is unchanged. (#4314)
* `ConvQuadInterp3d` / `conv_quad_interp3d` no longer return NaN gradients for `float16` input. The
Hessian determinant `_solve_cramer_sym3x3` divides by is a product of three second derivatives, so for
a `[0, 1]` response it lands around 1e-4 and below and still clears the `eps` gate. The forward divides
diff --git a/kornia/geometry/camera/pinhole.py b/kornia/geometry/camera/pinhole.py
index c56658146..a05276ed6 100644
--- a/kornia/geometry/camera/pinhole.py
+++ b/kornia/geometry/camera/pinhole.py
@@ -695,7 +695,7 @@ def pixel2cam(depth: torch.Tensor, intrinsics_inv: torch.Tensor, pixel_coords: t
torch.Tensor of shape BxHxWx3 with (x, y, z) cam coordinates.
"""
- if not len(depth.shape) == 4 and depth.shape[1] == 1:
+ if not (len(depth.shape) == 4 and depth.shape[1] == 1):
raise ValueError(f"Input depth has to be in the shape of Bx1xHxW. Got {depth.shape}")
if not len(intrinsics_inv.shape) == 3:
raise ValueError(f"Input intrinsics_inv has to be in the shape of Bx4x4. Got {intrinsics_inv.shape}")
diff --git a/tests/geometry/camera/test_pinhole.py b/tests/geometry/camera/test_pinhole.py
index 281644ce6..dbad42ead 100644
--- a/tests/geometry/camera/test_pinhole.py
+++ b/tests/geometry/camera/test_pinhole.py
@@ -135,6 +135,15 @@ class TestCam2Pixel(BaseTester):
class TestPixel2Cam(BaseTester):
+ @pytest.mark.parametrize("depth_shape", [(), (2,), (2, 1, 3), (2, 2, 3, 4), (2, 3, 3, 4), (2, 1, 3, 4, 1)])
+ def test_invalid_depth_shape(self, depth_shape, device, dtype):
+ depth = torch.ones(depth_shape, device=device, dtype=dtype)
+ intrinsics_inv = torch.eye(4, device=device, dtype=dtype).repeat(2, 1, 1)
+ pixel_coords = torch.ones(2, 3, 4, 3, device=device, dtype=dtype)
+
+ with pytest.raises(ValueError, match="Input depth has to be in the shape of Bx1xHxW"):
+ kornia.geometry.camera.pixel2cam(depth, intrinsics_inv, pixel_coords)
+
def _create_intrinsics(self, batch_size, fx, fy, cx, cy, device, dtype):
temp = torch.eye(4, device=device, dtype=dtype)
temp[0, 0], temp[0, 2] = fx, cx