Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): return a floating matrix for integer points in get_perspective_transform (#6405)

Lucac4288cfeffa2
diff --git a/changelog.d/6405.fixed.md b/changelog.d/6405.fixed.md
new file mode 100644
index 000000000..4bc140745
--- /dev/null
+++ b/changelog.d/6405.fixed.md
@@ -0,0 +1 @@
+`get_perspective_transform` and `get_perspective_transform3d` now return a matrix in the default floating dtype for integer or boolean points instead of truncating it to the input dtype.
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index f0e2d781f..e3b6e1f7b 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -980,6 +980,7 @@ def get_perspective_transform(points_src: torch.Tensor, points_dst: torch.Tensor
``float32`` (`#6435 <https://github.com/kornia/kornia/issues/6435>`_);
:func:`~kornia.geometry.homography.find_homography_dlt` returns it, at another scale
- float16 and bfloat16 points are solved in float32, and the result is cast back to their dtype
+ - integer or boolean points give a matrix in the default floating dtype; floating points keep theirs
- a caller's autocast region does not change the result, outside TorchScript
Args:
@@ -1008,6 +1009,13 @@ def get_perspective_transform(points_src: torch.Tensor, points_dst: torch.Tensor
KORNIA_CHECK(points_src.shape == points_dst.shape, "Source data shape must match Destination data shape.")
KORNIA_CHECK(points_src.dtype == points_dst.dtype, "Source data type must match Destination data type.")
+ # A homography is not integer-valued in general, so integer or boolean points are solved and returned in the
+ # default floating dtype (read off an empty tensor: TorchScript has no ``torch.get_default_dtype()``) rather than
+ # truncated back to the input dtype.
+ if not points_src.is_floating_point():
+ work_dtype = torch.empty(0, device=points_src.device).dtype
+ points_src, points_dst = points_src.to(work_dtype), points_dst.to(work_dtype)
+
# Solve via two closed-form unit-square (Heckbert) decompositions rather than an 8x8
# ``torch.linalg.solve``. Both yield the perspective transform mapping ``points_src`` onto
# ``points_dst``; the closed form additionally needs no LAPACK/cusolver backend (so it runs on
@@ -1913,6 +1921,7 @@ def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.T
``101`` and ``011`` in :func:`~kornia.geometry.bbox.bbox_generator3d` order; points 3, 4 and 6 are not
used, so they map onto their ``dst`` only when all eight pairs are related by one homography
- returns the source→destination **pixel** homography ``(B, 4, 4)``
+ - integer or boolean points give a matrix in the default floating dtype; floating points keep theirs
Args:
src: coordinates of the box corners in the source volume with shape ``(B, 8, 3)``.
@@ -1949,6 +1958,12 @@ def get_perspective_transform3d(src: torch.Tensor, dst: torch.Tensor) -> torch.T
f"with the same dtype (Got {src.dtype}, {dst.dtype})."
)
+ # A homography is not integer-valued in general, so integer or boolean points are solved and returned in the
+ # default floating dtype rather than truncated back to the input dtype.
+ if not src.is_floating_point():
+ work_dtype = torch.empty(0, device=src.device).dtype
+ src, dst = src.to(work_dtype), dst.to(work_dtype)
+
# we build the square matrix A from 5 point correspondences, 3 equations each, and solve it exactly
p = []
diff --git a/tests/geometry/transform/test_imgwarp.py b/tests/geometry/transform/test_imgwarp.py
index 918578fba..319bdbc1d 100644
--- a/tests/geometry/transform/test_imgwarp.py
+++ b/tests/geometry/transform/test_imgwarp.py
@@ -361,6 +361,16 @@ class TestGetPerspectiveTransform(BaseTester):
self.assert_close(kornia.geometry.transform_points(dst_trans_src, point_left), point_right)
+ def test_integer_points_6396(self, device):
+ points_src = torch.tensor([[[0, 0], [10, 0], [10, 10], [0, 10]]], device=device)
+ points_dst = torch.tensor([[[1, 2], [12, 1], [11, 13], [0, 9]]], device=device)
+
+ out = kornia.geometry.get_perspective_transform(points_src, points_dst)
+ expected = kornia.geometry.get_perspective_transform(points_src.to(out.dtype), points_dst.to(out.dtype))
+
+ assert out.dtype == torch.get_default_dtype()
+ self.assert_close(out, expected)
+
def test_dynamo(self, device, dtype, torch_optimizer):
points_src = torch.rand(1, 4, 2, device=device, dtype=dtype)
points_dst = torch.rand(1, 4, 2, device=device, dtype=dtype)
diff --git a/tests/geometry/transform/test_imgwarp3d.py b/tests/geometry/transform/test_imgwarp3d.py
index 2762d1233..76fd75e28 100644
--- a/tests/geometry/transform/test_imgwarp3d.py
+++ b/tests/geometry/transform/test_imgwarp3d.py
@@ -975,6 +975,18 @@ class TestPerspectiveTransform3D(BaseTester):
# compute gradient check
self.gradcheck(kornia.geometry.transform.get_perspective_transform3d, (points_src, points_dst), fast_mode=False)
+ def test_integer_points_6396(self, device):
+ points_src = torch.tensor(self._corners, device=device)[None] * 4
+ points_dst = points_src // 2 + torch.tensor([1, 2, 3], device=device)
+
+ out = kornia.geometry.transform.get_perspective_transform3d(points_src, points_dst)
+ expected = kornia.geometry.transform.get_perspective_transform3d(
+ points_src.to(out.dtype), points_dst.to(out.dtype)
+ )
+
+ assert out.dtype == torch.get_default_dtype()
+ self.assert_close(out, expected)
+
def test_convention_uses_corners_0_1_2_5_7(self, device, dtype):
# The 15 unknowns are solved exactly from corners 0, 1, 2, 5 and 7 (000, 100, 110, 101, 011); the
# destinations of corners 3, 4 and 6 do not enter the solve.