π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): pair each segment's own endpoints in find_homography_lines_dlt (#5008)
Mohammad Hijjawi11c0ea785014
diff --git a/changelog.d/5008.fixed.md b/changelog.d/5008.fixed.mdnew file mode 100644index 000000000..3e25facea--- /dev/null+++ b/changelog.d/5008.fixed.md@@ -0,0 +1 @@+`find_homography_lines_dlt` builds each segment's equations from its own start and end instead of from flattened points i and N + i, so line segments whose endpoints are not point correspondences give the right homography, `RANSAC("homography_from_linesegments")` finds a model on them, and a zero weight removes its segment (#4866).diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.pyindex c8ea45454..fa7fdc29f 100644--- a/kornia/geometry/homography.py+++ b/kornia/geometry/homography.py@@ -473,10 +473,8 @@ def find_homography_lines_dlt(Convention:- ``H`` maps image-1 points to image-2 points, as in :func:`find_homography_dlt`. Each segment is a``[start, end]`` pair of ``(x, y)`` points, and ``weights`` has one entry per segment.- - Known defect: each segment's equations are built from endpoints of two different segments, not from- its own start and end, so the estimate is correct only when the endpoints are themselves point- correspondences, and a zero weight does not remove its segment- (`#4866 <https://github.com/kornia/kornia/issues/4866>`_).+ - Both endpoints of image-1 segment ``i`` are constrained to lie, after mapping by ``H``, on the line+ through image-2 segment ``i``, so the endpoints need not be point correspondences.Args:ls1: A set of line segments in the first image with a tensor shape :math:`(B, N, 2, 2)`, or@@ -507,8 +505,11 @@ def find_homography_lines_dlt(endpoint_weights = weights.repeat_interleave(2, dim=1) if weights is not None else Nonepoints1_norm, transform1 = normalize_points(points1, weights=endpoint_weights)points2_norm, transform2 = normalize_points(points2, weights=endpoint_weights)- lst1, le1 = torch.chunk(points1_norm, dim=1, chunks=2)- lst2, le2 = torch.chunk(points2_norm, dim=1, chunks=2)+ # Pair each segment's own endpoints: the flattened points are [start_0, end_0, start_1, end_1, ...].+ segments1_norm = points1_norm.reshape(BS, N, 2, 2)+ segments2_norm = points2_norm.reshape(BS, N, 2, 2)+ lst1, le1 = segments1_norm[:, :, 0], segments1_norm[:, :, 1]+ lst2, le2 = segments2_norm[:, :, 0], segments2_norm[:, :, 1]xs1, ys1 = torch.chunk(lst1, dim=-1, chunks=2) # BxNx1xs2, ys2 = torch.chunk(lst2, dim=-1, chunks=2) # BxNx1@@ -557,10 +558,8 @@ def find_homography_lines_dlt_iterated(``e`` in the Gaussian kernel, the perpendicular distance in pixels of the mapped image-1 endpoints fromthe image-2 line: the residual of :func:`line_segment_transfer_error_one_way` divided by the image-2segment length it carries. A zero-length image-2 segment gets weight zero.- - Known defects: the endpoint pairing of :func:`find_homography_lines_dlt` and the length-scaled- residual of :func:`line_segment_transfer_error_one_way` still apply- (`#4866 <https://github.com/kornia/kornia/issues/4866>`_,- `#4867 <https://github.com/kornia/kornia/issues/4867>`_).+ - Known defect: the length-scaled residual of :func:`line_segment_transfer_error_one_way` still applies+ (`#4867 <https://github.com/kornia/kornia/issues/4867>`_).Args:ls1: A set of line segments in the first image with a tensor shape :math:`(B, N, 2, 2)`.diff --git a/kornia/geometry/ransac.py b/kornia/geometry/ransac.pyindex 9d9683996..3ac78f713 100644--- a/kornia/geometry/ransac.py+++ b/kornia/geometry/ransac.py@@ -81,9 +81,6 @@ class RANSAC(nn.Module):termination-length test; ``confidence=1`` runs the whole ``batch_size * max_iter`` budget.- A seeded call uses a private generator and leaves torch's global RNG state unchanged; ``seed=None``draws from the global generator.- - Known defects: for ``"homography_from_linesegments"``, the endpoint pairing of- :func:`~kornia.geometry.homography.find_homography_lines_dlt` applies- (`#4866 <https://github.com/kornia/kornia/issues/4866>`_).Args:model_type: "homography", "fundamental", "fundamental_7pt", "essential", ordiff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txtindex 90448071d..a2ffb9c0c 100644--- a/testing/half_precision_xfails/cpu_float16.txt+++ b/testing/half_precision_xfails/cpu_float16.txt@@ -367,8 +367,8 @@ call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomograpcall builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyDLTIter::test_clean_points[cpu-float16-2]call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyDLTIter::test_dirty_points_and_gradcheck[cpu-float16-1]call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyDLTIter::test_dirty_points_and_gradcheck[cpu-float16-2]-call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points[cpu-float16-1]call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points[cpu-float16-2]+call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points[cpu-float16-5]call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-float16-2]call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-float16-5]call builtins.AssertionError tests/geometry/test_line.py::TestFitLine::test_fit_line2[cpu-float16]diff --git a/tests/geometry/test_homography.py b/tests/geometry/test_homography.pyindex 61679c67d..74a37cb93 100644--- a/tests/geometry/test_homography.py+++ b/tests/geometry/test_homography.py@@ -469,7 +469,6 @@ class TestFindHomographyFromLinesDLT(BaseTester):segments2 = segments1 * torch.tensor([1.1, 0.9], device=device, dtype=dtype) + 7.0segments2 = segments2 + torch.arange(32, device=device, dtype=dtype).reshape(1, 8, 2, 2) * 0.03weights = torch.tensor([[0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], device=device, dtype=dtype)- # #4866 pairs endpoints incorrectly; these two zero weights isolate the normalization defect here.outlier1, outlier2 = segments1.clone(), segments2.clone()outlier1[:, 0] += torch.tensor([5000.0, -3000.0], device=device, dtype=dtype)outlier2[:, 0] += torch.tensor([-4000.0, 6000.0], device=device, dtype=dtype)@@ -1006,25 +1005,26 @@ class TestConventionHomography(BaseTester):assert _transfer_max(H, p1, p2) < 1e-2assert _transfer_max(H, p2, p1) > 100.0- def test_wart_find_homography_lines_dlt_endpoint_pairing_4866(self, device, dtype):+ def test_convention_find_homography_lines_dlt_endpoint_pairing_4866(self, device, dtype):_skip_half(dtype, _HALF_LINES)p1, p2, H_true = _planar(device, dtype)ls1, ls2 = p1.reshape(1, 6, 2, 2), p2.reshape(1, 6, 2, 2)+ tol = 1e-6 if dtype == torch.float64 else 5e-2# Slide every image-2 endpoint along its segment's true line: H_true still maps each image-1 segment onto its# image-2 line, but the endpoints are no longer point correspondences.d = ls2[:, :, 1] - ls2[:, :, 0]slid = torch.stack([ls2[:, :, 0] + 0.3 * d, ls2[:, :, 1] + 0.25 * d], dim=2)assert line_segment_transfer_error_one_way(ls1, slid, H_true).max() < 1e-2- # #4866: the equations of segment i use flattened points i and N + i, so the estimate misses by thousands- # of pixels here. Once the endpoints are paired per segment it recovers H_true.- assert _transfer_max(find_homography_lines_dlt(ls1, slid), p1, p2) > 100.0- # For the same reason a zero weight does not remove its segment: the outlier still moves the estimate.+ # The equations of segment i come from its own start and end (#4866 paired flattened points i and N + i and+ # missed by thousands of pixels here), so the lines alone recover H_true.+ assert _transfer_max(find_homography_lines_dlt(ls1, slid), p1, p2) < tol+ # A zero weight removes its segment: the outlier no longer moves the estimate.ls2_out = ls2.clone()ls2_out[0, 3] += torch.tensor([[35.0, -20.0], [-15.0, 30.0]], device=device, dtype=dtype)weights = torch.ones(1, 6, device=device, dtype=dtype)+ assert _transfer_max(find_homography_lines_dlt(ls1, ls2_out), p1, p2) > 1.0weights[0, 3] = 0.0- clean = (torch.arange(12, device=device) != 6) & (torch.arange(12, device=device) != 7)- assert _transfer_max(find_homography_lines_dlt(ls1, ls2_out, weights), p1[:, clean], p2[:, clean]) > 10.0+ assert _transfer_max(find_homography_lines_dlt(ls1, ls2_out, weights), p1, p2) < toldef test_wart_line_segment_error_scales_with_length_4867(self, device, dtype):_skip_half(dtype, _HALF_LINES)