π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Fix homography between unit square and scaled version (#3335)
manurare8ca3e6b7f0ea
diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.pyindex 7581f8b95..3f455d384 100644--- a/kornia/geometry/homography.py+++ b/kornia/geometry/homography.py@@ -180,16 +180,16 @@ def find_homography_dlt(A = torch.cat((ax, ay), dim=-1).reshape(ax.shape[0], -1, ax.shape[-1])if weights is None:- # All points are equally important- A = A.transpose(-2, -1) @ A+ Aw = Aelse:# We should use provided weightsif not (len(weights.shape) == 2 and weights.shape == points1.shape[:2]):raise AssertionError(weights.shape)- w_full = weights.repeat_interleave(2, dim=1).unsqueeze(1)- A = (A.transpose(-2, -1) * w_full) @ A+ w_full = weights.repeat_interleave(2, dim=1)+ Aw = torch.einsum("...ij,...i->...ij", A, w_full)if solver == "svd":+ A = A.transpose(-2, -1) @ Awtry:_, _, V = _torch_svd_cast(A)except RuntimeError:@@ -197,8 +197,14 @@ def find_homography_dlt(return torch.empty((points1_norm.size(0), 3, 3), device=device, dtype=dtype)H = V[..., -1].view(-1, 3, 3)elif solver == "lu":- B = torch.ones(A.shape[0], A.shape[1], device=device, dtype=dtype)+ if points1.shape[1] > 4:+ A = A.transpose(-2, -1) @ Aw+ B = torch.ones(A.shape[0], A.shape[1], device=device, dtype=dtype)+ else:+ B = -Aw[..., -1]+ A = Aw[..., :-1]sol, _, _ = safe_solve_with_mask(B, A)+ sol = torch.nn.functional.pad(sol, (0, 0, 0, 1), value=1) if sol.shape[1] == 8 else solH = sol.reshape(-1, 3, 3)else:raise NotImplementedErrordiff --git a/tests/geometry/test_homography.py b/tests/geometry/test_homography.pyindex 17a10e0ec..979a0a018 100644--- a/tests/geometry/test_homography.py+++ b/tests/geometry/test_homography.py@@ -208,6 +208,12 @@ class TestFindHomographyDLT(BaseTester):assert H_withweights.shape == (B, 3, 3)self.assert_close(H_noweights, H_withweights, rtol=1e-3, atol=1e-4)+ def test_scaled_fixed_points(self, device, dtype):+ points1 = torch.tensor([[[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]], device=device, dtype=dtype)+ points2 = points1 * 100+ H = find_homography_dlt(points1, points2, None, "lu")+ assert not torch.isnan(H).any()+@pytest.mark.parametrize("batch_size", [1, 2, 5])def test_clean_points_svd(self, batch_size, device, dtype):# generate input data