Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): reject mismatched image and depth sizes in warp_frame_depth (#4861)

Ethan Stoner928e41c7335c
diff --git a/changelog.d/4861.fixed.md b/changelog.d/4861.fixed.md
new file mode 100644
index 000000000..52cbec9ad
--- /dev/null
+++ b/changelog.d/4861.fixed.md
@@ -0,0 +1 @@
+`warp_frame_depth` now raises when `image_src` and `depth_dst` have different heights or widths, instead of resampling the whole image onto the depth grid.
diff --git a/kornia/geometry/depth.py b/kornia/geometry/depth.py
index 305c5229f..f95f9a551 100644
--- a/kornia/geometry/depth.py
+++ b/kornia/geometry/depth.py
@@ -402,6 +402,11 @@ def warp_frame_depth(
KORNIA_CHECK_SHAPE(depth_dst, ["B", "1", "H", "W"])
KORNIA_CHECK_SHAPE(src_trans_dst, ["B", "4", "4"])
KORNIA_CHECK_SHAPE(camera_matrix, ["B", "3", "3"])
+ KORNIA_CHECK(
+ image_src.shape[-2:] == depth_dst.shape[-2:],
+ f"image_src and depth_dst must have the same height and width. "
+ f"Got {list(image_src.shape[-2:])} and {list(depth_dst.shape[-2:])}.",
+ )
if (
image_src.shape[0] == 0
diff --git a/tests/geometry/test_depth.py b/tests/geometry/test_depth.py
index 2acc21c5e..45a270b4b 100644
--- a/tests/geometry/test_depth.py
+++ b/tests/geometry/test_depth.py
@@ -543,9 +543,22 @@ class TestWarpFrameDepth(BaseTester):
image_dst = kornia.geometry.depth.warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)
assert image_dst.shape == (1, 2, height, width)
+ @pytest.mark.parametrize("batch_size", [0, 1])
+ @pytest.mark.parametrize("depth_hw", [(3, 4), (3, 8), (6, 4)])
+ def test_exception_image_depth_size_mismatch(self, batch_size, depth_hw, device, dtype):
+ # kornia#4800: a depth map of another size used to resample the whole image onto its own grid. A mismatch
+ # in the height alone or the width alone is rejected too.
+ image_src = torch.rand(batch_size, 1, 6, 8, device=device, dtype=dtype)
+ depth_dst = torch.ones(batch_size, 1, *depth_hw, device=device, dtype=dtype)
+ src_trans_dst = torch.eye(4, device=device, dtype=dtype).repeat(batch_size, 1, 1)
+ camera_matrix = torch.eye(3, device=device, dtype=dtype).repeat(batch_size, 1, 1)
+
+ with pytest.raises(BaseError, match="same height and width"):
+ warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)
+
def test_empty_batch_4281(self, device, dtype):
# Regression for kornia#4281: output geometry comes from the destination depth map.
- image_src = torch.zeros(0, 3, 2, 3, device=device, dtype=dtype, requires_grad=True)
+ image_src = torch.zeros(0, 3, 4, 5, device=device, dtype=dtype, requires_grad=True)
depth_dst = torch.zeros(0, 1, 4, 5, device=device, dtype=dtype)
src_trans_dst = torch.zeros(0, 4, 4, device=device, dtype=dtype)
camera_matrix = torch.zeros(0, 3, 3, device=device, dtype=dtype)