Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): clarify stereo disparity layout error (#4380)

Zhenhuan Wangb8404f89f241
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4df25d2d7..8fb94185c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -414,6 +414,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `rad2deg` and `deg2rad` now handle integer tensor inputs correctly and preserve
float64 precision. `angle_to_rotation_matrix` inherits the corrected conversion,
while the implementation preserves ONNX export compatibility. (#4358)
+* Corrected stereo disparity validation errors to describe the required channels-last
+ `(B, H, W, 1)` layout and report the received shape. (#4380)
* `CameraModelBase.__init__` now validates `params` against the shape it documents, instead of storing
whatever it is given. The typed constructors (`PinholeModel`, `BrownConradyModel`, `KannalaBrandtK3`,
`Orthographic`) each apply the same two comparisons, so only the direct-construction path -- which is
diff --git a/kornia/geometry/camera/stereo.py b/kornia/geometry/camera/stereo.py
index 98c9d0361..9ffe1e2aa 100644
--- a/kornia/geometry/camera/stereo.py
+++ b/kornia/geometry/camera/stereo.py
@@ -342,7 +342,8 @@ def _check_disparity_tensor(disparity_tensor: torch.Tensor) -> None:
if disparity_tensor.shape[-1] != 1:
raise StereoException(
- "Expected dimension 1 of 'disparity_tensor' to be 1 for as single channeled disparity map."
+ "Expected 'disparity_tensor' to have channels-last shape (B, H, W, 1) "
+ "with a single channel in the last dimension. "
f"Got {disparity_tensor.shape}."
)
diff --git a/tests/geometry/camera/test_stereo.py b/tests/geometry/camera/test_stereo.py
index 83b1077e5..209fd6d7e 100644
--- a/tests/geometry/camera/test_stereo.py
+++ b/tests/geometry/camera/test_stereo.py
@@ -186,6 +186,25 @@ class _SmokeTestData:
class TestStereoCamera(BaseTester):
"""Test class for :class:`~kornia.geometry.camera.stereo.StereoCamera`"""
+ @pytest.mark.parametrize("disparity_shape", [(1, 1, 3, 5), (2, 3, 5, 2)])
+ @pytest.mark.parametrize("entrypoint", ["method", "function"])
+ def test_reproject_disparity_layout_error_4374(self, disparity_shape, entrypoint, device, dtype):
+ left, right = _SmokeTestData._create_stereo_camera(disparity_shape[0], device, dtype, tx_fx=-10)
+ camera = StereoCamera(left, right)
+ disparity = torch.ones(disparity_shape, device=device, dtype=dtype)
+
+ with pytest.raises(StereoException) as exc_info:
+ if entrypoint == "method":
+ camera.reproject_disparity_to_3D(disparity)
+ else:
+ reproject_disparity_to_3D(disparity, camera.Q)
+
+ assert str(exc_info.value).splitlines()[0] == (
+ "Expected 'disparity_tensor' to have channels-last shape (B, H, W, 1) "
+ "with a single channel in the last dimension. "
+ f"Got {disparity.shape}."
+ )
+
@staticmethod
def _create_disparity_tensor(batch_size, height, width, max_disparity, device, dtype):
size = (batch_size, height, width, 1)
@@ -294,8 +313,8 @@ class TestStereoCamera(BaseTester):
# cloud is (B, H, W, 3). The channels-FIRST (B, 1, H, W) layout that the rest of kornia uses for images
# is rejected, and so is an unbatched (B, H, W). The method's docstring used to say (B, 1, H, W) -- the
# layout the shared guard rejects -- and now says (B, H, W, 1), matching the module-level function and
- # the private guard's own docstring. The guard's message still says "dimension 1" for a shape[-1] check
- # and never names (B, H, W, 1); that is kornia#4374, and the match below is on the current text.
+ # the private guard's own docstring. The message names the required layout and the last-dimension
+ # channel check (regression for kornia#4374).
# The shape claim carries a value so it cannot pass on a dummy: with fx = 100 and tx = 0.5, a disparity
# of 10 puts every point at Z = fx * tx / d = 5.
# Snippet used to generate expected: cam.reproject_disparity_to_3D(full((1, 3, 5, 1), 10.0)) executed
@@ -307,7 +326,7 @@ class TestStereoCamera(BaseTester):
points = cam.reproject_disparity_to_3D(disparity)
assert points.shape == (1, 3, 5, 3)
self.assert_close(points[0, 0, 0, 2], torch.tensor(5.0, device=device, dtype=dtype))
- with pytest.raises(StereoException, match="to be 1 for as single channeled disparity map"):
+ with pytest.raises(StereoException, match=r"channels-last shape \(B, H, W, 1\)"):
cam.reproject_disparity_to_3D(torch.full((1, 1, 3, 5), 10.0, device=device, dtype=dtype))
with pytest.raises(StereoException, match="to have 4 dimensions"):
cam.reproject_disparity_to_3D(torch.full((1, 3, 5), 10.0, device=device, dtype=dtype))