π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): reproject an empty batch of disparity maps in StereoCamera (#5979)
Saksham Sharmafcc05a32407a
diff --git a/changelog.d/5979.fixed.md b/changelog.d/5979.fixed.mdnew file mode 100644index 000000000..6dcbff006--- /dev/null+++ b/changelog.d/5979.fixed.md@@ -0,0 +1 @@+`StereoCamera.reproject_disparity_to_3D` and `kornia.geometry.camera.stereo.reproject_disparity_to_3D` return an empty `(0, H, W, 3)` point cloud for an empty batch of disparity maps, instead of raising a `reshape` error. An empty rig has been accepted since #4281.diff --git a/kornia/geometry/camera/stereo.py b/kornia/geometry/camera/stereo.pyindex 0ea68afe3..f78b8b6b9 100644--- a/kornia/geometry/camera/stereo.py+++ b/kornia/geometry/camera/stereo.py@@ -391,7 +391,8 @@ def reproject_disparity_to_3D(disparity_tensor: torch.Tensor, Q_matrix: torch.Te# the two pixel indices in the result.u, v = torch.unbind(uv, dim=-1)u, v = torch.unsqueeze(u, -1), torch.unsqueeze(v, -1)- uvd = torch.stack((u, v, disparity_tensor), 1).reshape(batch_size, 3, -1).permute(0, 2, 1)+ # The pixel count is spelled out: a ``-1`` cannot be inferred for an empty batch.+ uvd = torch.stack((u, v, disparity_tensor), 1).reshape(batch_size, 3, rows * cols).permute(0, 2, 1)points = transform_points(Q_matrix, uvd).reshape(batch_size, rows, cols, 3)# Final check that everything went well.diff --git a/tests/geometry/camera/test_stereo.py b/tests/geometry/camera/test_stereo.pyindex c6e0be98d..78cda88fc 100644--- a/tests/geometry/camera/test_stereo.py+++ b/tests/geometry/camera/test_stereo.py@@ -260,6 +260,26 @@ class TestStereoCamera(BaseTester):assert stereo_camera.Q.dtype == dtypeassert stereo_camera.Q.device == device+ @pytest.mark.parametrize("size", [(4, 5), (0, 5), (0, 0)])+ @pytest.mark.parametrize("entrypoint", ["method", "function"])+ def test_reproject_disparity_empty_batch_4281(self, size, entrypoint, device, dtype):+ # The empty rig of kornia#4281 also reprojects an empty batch of disparity maps.+ stereo_camera = StereoCamera(*_SmokeTestData._create_stereo_camera(0, device, dtype, tx_fx=-10))+ disparity = torch.rand(0, *size, 1, device=device, dtype=dtype, requires_grad=True)++ xyz = (+ stereo_camera.reproject_disparity_to_3D(disparity)+ if entrypoint == "method"+ else reproject_disparity_to_3D(disparity, stereo_camera.Q)+ )++ assert xyz.shape == (0, *size, 3)+ assert xyz.dtype == dtype+ assert xyz.device == device+ xyz.sum().backward()+ assert disparity.grad is not None+ assert disparity.grad.shape == disparity.shape+def test_stereo_camera_attributes_real(self, batch_size, device, dtype):"""Test proper setup of the class for real data."""left_rectified_camera, right_rectified_camera = _RealTestData._get_real_stereo_camera(batch_size, device, dtype)