Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): accept unbatched segments in find_homography_lines_dlt_iterated (#5996)

Saksham Sharma92e63a12adee
diff --git a/changelog.d/5996.fixed.md b/changelog.d/5996.fixed.md
new file mode 100644
index 000000000..a8757fad8
--- /dev/null
+++ b/changelog.d/5996.fixed.md
@@ -0,0 +1 @@
+`find_homography_lines_dlt_iterated` accepts unbatched `(N, 2, 2)` line segments and treats them as `B = 1`, as `find_homography_lines_dlt` does. Previously it solved once and then raised a `ShapeError` from `line_segment_transfer_error_one_way` in its re-weighting step.
diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.py
index b7341b0a6..26d630b55 100644
--- a/kornia/geometry/homography.py
+++ b/kornia/geometry/homography.py
@@ -878,8 +878,10 @@ def find_homography_lines_dlt_iterated(
the image-2 line. A zero-length image-2 segment gets weight zero.
Args:
- ls1: A set of line segments in the first image with a tensor shape :math:`(B, N, 2, 2)`.
- ls2: A set of line segments in the second image with a tensor shape :math:`(B, N, 2, 2)`.
+ ls1: A set of line segments in the first image with a tensor shape :math:`(B, N, 2, 2)`, or
+ :math:`(N, 2, 2)`, which is treated as :math:`B = 1`, as in :func:`find_homography_lines_dlt`.
+ ls2: A set of line segments in the second image with a tensor shape :math:`(B, N, 2, 2)`, or
+ :math:`(N, 2, 2)`, which is treated as :math:`B = 1`.
weights: Tensor containing the weights per segment with a shape of :math:`(B, N)`.
Used for the first iteration of the IRWLS.
soft_inl_th: standard deviation, in pixels, of the Gaussian re-weighting kernel of
@@ -893,6 +895,11 @@ def find_homography_lines_dlt_iterated(
AssertionError: if fewer than four segments are given, as in :func:`find_homography_lines_dlt`.
"""
+ # Promote unbatched segments as find_homography_lines_dlt does: the re-weighting below needs the batch axis.
+ if len(ls1.shape) == 3:
+ ls1 = ls1[None]
+ if len(ls2.shape) == 3:
+ ls2 = ls2[None]
H: torch.Tensor = find_homography_lines_dlt(ls1, ls2, weights)
for _ in range(n_iter - 1):
squared_distances: torch.Tensor = _line_segment_squared_distance_one_way(ls1, ls2, H)
diff --git a/tests/geometry/test_homography.py b/tests/geometry/test_homography.py
index 3472a0145..d5b6fe54b 100644
--- a/tests/geometry/test_homography.py
+++ b/tests/geometry/test_homography.py
@@ -995,6 +995,19 @@ class TestFindHomographyFromLinesDLT(BaseTester):
kornia.geometry.transform_points(dst_homo_src, points_src_st), points_dst_st, rtol=rtol, atol=atol
)
+ @pytest.mark.parametrize("use_weights", [False, True])
+ def test_iterated_accepts_unbatched_segments_5995(self, use_weights, device, dtype):
+ # (N, 2, 2) is treated as B = 1, as in find_homography_lines_dlt. The re-weighting used to reject it with
+ # a ShapeError from line_segment_transfer_error_one_way after the first solve.
+ ls1 = torch.rand(6, 2, 2, device=device, dtype=dtype) * 100
+ ls2 = ls1 + torch.rand(6, 2, 2, device=device, dtype=dtype)
+ weights = torch.ones(1, 6, device=device, dtype=dtype) if use_weights else None
+
+ H = find_homography_lines_dlt_iterated(ls1, ls2, weights)
+
+ assert H.shape == (1, 3, 3)
+ self.assert_close(H, find_homography_lines_dlt_iterated(ls1[None], ls2[None], weights), rtol=0.0, atol=0.0)
+
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_clean_points_iter(self, batch_size, device, dtype):
# generate input data