Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(calibration): broadcast undistort_points' sensor tilt against the points like the other coefficients (#6459)

Dmytro Mishkin10bde4c1cf26
diff --git a/changelog.d/6459.fixed.md b/changelog.d/6459.fixed.md
new file mode 100644
index 000000000..f6367cfd4
--- /dev/null
+++ b/changelog.d/6459.fixed.md
@@ -0,0 +1 @@
+`undistort_points` with 14 distortion coefficients applies each camera's sensor tilt to its own points when the cameras broadcast against points with more than three axes, such as `(T, B, N, 2)` points with `(B, 3, 3)` cameras and `(B, 14)` coefficients, or their `K[None]` and `dist[None]` forms. The tilt step paired its transforms with the first axis of the points while the other coefficients broadcast, so such calls returned wrong points, or raised a `ValueError` when the first axis did not match. Calls that already gave the right result are unchanged bit for bit.
diff --git a/kornia/geometry/calibration/undistort.py b/kornia/geometry/calibration/undistort.py
index ccbe3fba7..1ea70a01e 100644
--- a/kornia/geometry/calibration/undistort.py
+++ b/kornia/geometry/calibration/undistort.py
@@ -118,11 +118,16 @@ def undistort_points(
# coefficient count, not its values, so calibration can learn tilt from a zero initialization.
if has_tilt:
inv_tilt = tilt_projection(dist[..., 12:13], dist[..., 13:14], True)
- if inv_tilt.dim() == 2:
- inv_tilt = inv_tilt.unsqueeze(0)
+ xy = torch.stack([x, y], dim=-1)
+ # transform_points pairs a batch of transforms with the leading axis of the points, so give it one transform
+ # per point set: the tilt then broadcasts against the points like the other coefficients.
+ # (TorchScript has no torch.broadcast_shapes, so broadcast 1 x 1 corners for the batch shape.)
+ batch_shape = list(torch.broadcast_tensors(xy[..., :1, :1], inv_tilt[..., :1, :1])[0].shape[:-2])
+ inv_tilt = inv_tilt.expand(batch_shape + [3, 3])
+ xy = xy.expand(batch_shape + list(xy.shape[-2:]))
# Transposed untilt points (instead of [x,y,1]^T, we obtain [x,y,1])
- x, y = transform_points(inv_tilt, torch.stack([x, y], dim=-1)).unbind(-1)
+ x, y = transform_points(inv_tilt, xy).unbind(-1)
# Iteratively undistort points
x0, y0 = x, y
diff --git a/tests/geometry/calibration/test_undistort.py b/tests/geometry/calibration/test_undistort.py
index ed7311a39..fdc26fc36 100644
--- a/tests/geometry/calibration/test_undistort.py
+++ b/tests/geometry/calibration/test_undistort.py
@@ -91,6 +91,53 @@ class TestUndistortPoints(BaseTester):
assert actual.shape == points.shape
self.assert_close(actual, expected)
+ @pytest.mark.parametrize("num_frames", [2, 3])
+ @pytest.mark.parametrize("camera_axis", [False, True])
+ def test_tilt_broadcasts_like_the_other_coefficients(self, num_frames, camera_axis, device, dtype):
+ # (T, B, N, 2) points against (B, 3, 3) cameras and (B, 14) coefficients, or their [None] forms, pair camera
+ # b with axis 1, as they do without tilt. The tilt step used to pair its transforms with axis 0 instead: a
+ # wrong result for T == B or a [None] camera axis, and a ValueError otherwise.
+ num_cams, num_points = 2, 5
+ points = torch.rand(num_frames, num_cams, num_points, 2, device=device, dtype=dtype) * 8
+ K = torch.tensor(
+ [
+ [[100.0, 0.0, 4.0], [0.0, 90.0, 3.0], [0.0, 0.0, 1.0]],
+ [[80.0, 0.0, 2.0], [0.0, 70.0, 5.0], [0.0, 0.0, 1.0]],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ dist = torch.zeros(num_cams, 14, device=device, dtype=dtype)
+ dist[:, 0] = torch.tensor([0.1, -0.05], device=device, dtype=dtype)
+ dist[:, 12] = torch.tensor([0.1, -0.2], device=device, dtype=dtype)
+ dist[:, 13] = torch.tensor([-0.15, 0.05], device=device, dtype=dtype)
+ dist.requires_grad_()
+
+ actual = undistort_points(points, K[None], dist[None]) if camera_axis else undistort_points(points, K, dist)
+ expected = torch.stack(
+ [
+ torch.stack([undistort_points(points[t, b], K[b], dist[b]) for b in range(num_cams)])
+ for t in range(num_frames)
+ ]
+ )
+
+ assert actual.shape == points.shape
+ # On MPS the batched half-precision call rounds differently from the per-camera calls, which moves a few
+ # values by 2-4 ulps over the iterations; a camera paired with the wrong axis moves them by pixels.
+ tol = {"rtol": 2e-2, "atol": 1e-2} if dtype in (torch.float16, torch.bfloat16) else {}
+ self.assert_close(actual, expected, **tol)
+ # Every frame shares a camera's coefficients, so their gradient sums over the frames, as the per-camera
+ # calls' gradients do. In half precision the tilt gradients of both forms are off the float64 gradient by
+ # up to its own magnitude, so only the wider dtypes can tell the two apart. In float32 the two forms differ
+ # by up to 3e-4 on gradients of magnitude ~5, measured over 150 seeds on CPU and MPS; swapping the cameras
+ # moves them by at least 0.58.
+ if dtype in (torch.float16, torch.bfloat16):
+ return
+ actual_grad = torch.autograd.grad(actual.sum(), dist)[0]
+ expected_grad = torch.autograd.grad(expected.sum(), dist)[0]
+ grad_tol = {"rtol": 1e-3, "atol": 2e-3} if dtype == torch.float32 else {}
+ self.assert_close(actual_grad, expected_grad, **grad_tol)
+
@pytest.mark.parametrize(
"batch_size, num_points, num_distcoeff", [(1, 3, 4), (2, 4, 5), (3, 5, 8), (4, 6, 12), (5, 7, 14)]
)