π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix ``undistort_image`` documentation and support additional dimensions. (#1464)
Callum Downiee77253f7976b
diff --git a/kornia/geometry/calibration/undistort.py b/kornia/geometry/calibration/undistort.pyindex 88063bddd..ddb317a46 100644--- a/kornia/geometry/calibration/undistort.py+++ b/kornia/geometry/calibration/undistort.py@@ -117,13 +117,13 @@ def undistort_image(image: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) ->Example:>>> img = torch.rand(1, 3, 5, 5)>>> K = torch.eye(3)[None]- >>> dist_coeff = torch.rand(4)+ >>> dist_coeff = torch.rand(1, 4)>>> out = undistort_image(img, K, dist_coeff)>>> out.shapetorch.Size([1, 3, 5, 5])"""- if len(image.shape) < 2:+ if len(image.shape) < 3:raise ValueError(f"Image shape is invalid. Got: {image.shape}.")if K.shape[-2:] != (3, 3):@@ -135,7 +135,17 @@ def undistort_image(image: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) ->if not image.is_floating_point():raise ValueError(f'Invalid input image data type. Input should be float. Got {image.dtype}.')- B, _, rows, cols = image.shape+ if image.shape[:-3] != K.shape[:-2] or image.shape[:-3] != dist.shape[:-1]:+ # Input with image shape (1, C, H, W), K shape (3, 3), dist shape (4)+ # allowed to avoid a breaking change.+ if not all((image.shape[:-3] == (1,), K.shape[:-2] == (), dist.shape[:-1] == ())):+ raise ValueError(+ f'Input shape is invalid. Input batch dimensions should match. '+ f'Got {image.shape[:-3]}, {K.shape[:-2]}, {dist.shape[:-1]}.'+ )++ channels, rows, cols = image.shape[-3:]+ B = image.numel() // (channels * rows * cols)# Create point coordinates for each pixel of the imagexy_grid: torch.Tensor = create_meshgrid(rows, cols, False, image.device, image.dtype)@@ -147,6 +157,6 @@ def undistort_image(image: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) ->mapy: torch.Tensor = ptsd[..., 1].reshape(B, rows, cols) # B x rows x cols, float# Remap image to undistort- out = remap(image, mapx, mapy, align_corners=True)+ out = remap(image.reshape(B, channels, rows, cols), mapx, mapy, align_corners=True)- return out+ return out.view_as(image)diff --git a/test/geometry/calibration/test_undistort.py b/test/geometry/calibration/test_undistort.pyindex 7eb910845..849882c05 100644--- a/test/geometry/calibration/test_undistort.py+++ b/test/geometry/calibration/test_undistort.py@@ -212,6 +212,54 @@ class TestUndistortImage:imu = undistort_image(im, K, distCoeff)assert imu.shape == (1, 3, 5, 5)+ def test_shape_minimum_dims(self, device, dtype):+ im = torch.rand(3, 5, 5, device=device, dtype=dtype)+ K = torch.rand(3, 3, device=device, dtype=dtype)+ distCoeff = torch.rand(4, device=device, dtype=dtype)++ imu = undistort_image(im, K, distCoeff)+ assert imu.shape == (3, 5, 5)++ def test_shape_extra_dims(self, device, dtype):+ im = torch.rand(1, 1, 3, 5, 5, device=device, dtype=dtype).tile(3, 2, 1, 1, 1)+ K = torch.rand(1, 1, 3, 3, device=device, dtype=dtype).tile(3, 2, 1, 1)+ distCoeff = torch.rand(1, 1, 4, device=device, dtype=dtype).tile(3, 2, 1)++ imu = undistort_image(im, K, distCoeff)+ assert imu.shape == (3, 2, 3, 5, 5)+ torch.testing.assert_allclose(imu[0], imu[1])++ def test_exception(self, device, dtype):+ with pytest.raises(ValueError):+ im = torch.rand(5, 5, device=device, dtype=dtype)+ K = torch.rand(3, 3, device=device, dtype=dtype)+ distCoeff = torch.rand(4, device=device, dtype=dtype)+ undistort_image(im, K, distCoeff)++ with pytest.raises(ValueError):+ im = torch.rand(3, 5, 5, device=device, dtype=dtype)+ K = torch.rand(4, 4, device=device, dtype=dtype)+ distCoeff = torch.rand(4, device=device, dtype=dtype)+ undistort_image(im, K, distCoeff)++ with pytest.raises(ValueError):+ im = torch.rand(3, 5, 5, device=device, dtype=dtype)+ K = torch.rand(3, 3, device=device, dtype=dtype)+ distCoeff = torch.rand(6, device=device, dtype=dtype)+ undistort_image(im, K, distCoeff)++ with pytest.raises(ValueError):+ im = torch.randint(0, 256, (3, 5, 5), device=device, dtype=torch.uint8)+ K = torch.rand(3, 3, device=device, dtype=dtype)+ distCoeff = torch.rand(4, device=device, dtype=dtype)+ undistort_image(im, K, distCoeff)++ with pytest.raises(ValueError):+ im = torch.rand(1, 1, 3, 5, 5, device=device, dtype=dtype)+ K = torch.rand(1, 3, 3, device=device, dtype=dtype)+ distCoeff = torch.rand(1, 4, device=device, dtype=dtype)+ undistort_image(im, K, distCoeff)+def test_opencv(self, device, dtype):im = torch.tensor([