Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(calibration)!: reject distort_points input whose trailing size is not 2 (#6469)

sarankumarc3c2adaacbf7
diff --git a/changelog.d/6469.breaking.md b/changelog.d/6469.breaking.md
new file mode 100644
index 000000000..f1841482a
--- /dev/null
+++ b/changelog.d/6469.breaking.md
@@ -0,0 +1 @@
+`distort_points` raises `ShapeError` when `points` is not `(*, N, 2)`, as `undistort_points` does. Points with a third column used to be truncated silently to their first two columns, and a single `(2,)` point used to come back as `(1, 2)`, or as `(B, 1, 2)` with batched `(B, 3, 3)` cameras; pass it as `(1, 2)` instead.
diff --git a/kornia/geometry/calibration/distort.py b/kornia/geometry/calibration/distort.py
index f2a2d7188..b2d1ee54c 100644
--- a/kornia/geometry/calibration/distort.py
+++ b/kornia/geometry/calibration/distort.py
@@ -20,6 +20,8 @@ from typing import Optional
import torch
import torch.nn.functional as F
+from kornia.core.check import KORNIA_CHECK_SHAPE
+
# Based on https://github.com/opencv/opencv/blob/master/modules/calib3d/src/distortion_model.hpp#L75
def tilt_projection(taux: torch.Tensor, tauy: torch.Tensor, return_inverse: bool = False) -> torch.Tensor:
@@ -145,8 +147,7 @@ def distort_points(
>>> points_dist = distort_points(points, K, dist_coeff)
"""
- if points.dim() < 2 and points.shape[-1] != 2:
- raise ValueError(f"points shape is invalid. Got {points.shape}.")
+ KORNIA_CHECK_SHAPE(points, ["*", "N", "2"])
if K.shape[-2:] != (3, 3):
raise ValueError(f"K matrix shape is invalid. Got {K.shape}.")
diff --git a/tests/geometry/calibration/test_distort.py b/tests/geometry/calibration/test_distort.py
index 4f717bc48..41b19efe4 100644
--- a/tests/geometry/calibration/test_distort.py
+++ b/tests/geometry/calibration/test_distort.py
@@ -256,6 +256,21 @@ class TestDistortPoints(BaseTester):
assert not torch.allclose(distort_points(points, K, short).float(), points.float())
assert torch.equal(distort_points(points, K, short), distort_points(points, K, padded))
+ def test_exception_rejects_points_whose_trailing_size_is_not_2(
+ self, device: torch.device, dtype: torch.dtype
+ ) -> None:
+ # The points must be (*, N, 2), as in undistort_points: a third column used to be dropped silently
+ # and an unbatched (2,) point used to come back as (1, 2).
+ from kornia.core.exceptions import ShapeError
+
+ K = torch.eye(3, device=device, dtype=dtype)
+ dist = torch.zeros(4, device=device, dtype=dtype)
+ for shape in ((5, 3), (1, 5, 3), (5, 1), (2,)):
+ points = torch.rand(shape, device=device, dtype=dtype)
+ with pytest.raises(ShapeError):
+ distort_points(points, K, dist)
+ assert distort_points(torch.rand(5, 2, device=device, dtype=dtype), K, dist).shape == (5, 2)
+
def test_convention_zero_coefficients_are_a_no_op_at_dtype_tolerance(self, device, dtype):
# With every coefficient zero, distort_points normalizes with new_K (= K here) and denormalizes with K,
# so it is a no-op to roundoff. The points sit half a focal length off the principal point.