Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Applies #690 - find_homography_dlt performance improvement and weight… (#693)

Danielf614c5f714ad
diff --git a/kornia/geometry/homography.py b/kornia/geometry/homography.py
index 923be9813..916d5c903 100644
--- a/kornia/geometry/homography.py
+++ b/kornia/geometry/homography.py
@@ -1,5 +1,7 @@
+import warnings
+
import torch
-from typing import Tuple
+from typing import Tuple, Optional
import kornia
from kornia.geometry.epipolar import normalize_points
@@ -7,7 +9,8 @@ from kornia.geometry.epipolar import normalize_points
TupleTensor = Tuple[torch.Tensor, torch.Tensor]
-def find_homography_dlt(points1: torch.Tensor, points2: torch.Tensor, weights: torch.Tensor) -> torch.Tensor:
+def find_homography_dlt(
+ points1: torch.Tensor, points2: torch.Tensor, weights: Optional[torch.Tensor] = None) -> torch.Tensor:
r"""Computes the homography matrix using the DLT formulation.
The linear system is solved by using the Weighted Least Squares Solution for the 4 Points algorithm.
@@ -15,7 +18,8 @@ def find_homography_dlt(points1: torch.Tensor, points2: torch.Tensor, weights: t
Args:
points1 (torch.Tensor): A set of points in the first image with a tensor shape :math:`(B, N, 2)`.
points2 (torch.Tensor): A set of points in the second image with a tensor shape :math:`(B, N, 2)`.
- weights (torch.Tensor): Tensor containing the weights per point correspondence with a shape of :math:`(B, N)`.
+ weights (torch.Tensor, optional): Tensor containing the weights per point correspondence with a shape of
+ :math:`(B, N)`. Defaults to all ones.
Returns:
torch.Tensor: the computed homography matrix with shape :math:`(B, 3, 3)`.
@@ -35,24 +39,23 @@ def find_homography_dlt(points1: torch.Tensor, points2: torch.Tensor, weights: t
# DIAPO 11: https://www.uio.no/studier/emner/matnat/its/nedlagte-emner/UNIK4690/v16/forelesninger/lecture_4_3-estimating-homographies-from-feature-correspondences.pdf # noqa: E501
ax = torch.cat([zeros, zeros, zeros, -x1, -y1, -ones, y2 * x1, y2 * y1, y2], dim=-1)
ay = torch.cat([x1, y1, ones, zeros, zeros, zeros, -x2 * x1, -x2 * y1, -x2], dim=-1)
+ 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
+ else:
+ # We should use provided weights
+ assert len(weights.shape) == 2 and weights.shape == points1.shape[:2], weights.shape
+ w_diag = torch.diag_embed(weights.repeat(1, 2))
+ A = A.transpose(-2, -1) @ w_diag @ A
- w_list = []
- axy_list = []
- for i in range(points1.shape[1]):
- axy_list.append(ax[:, i])
- axy_list.append(ay[:, i])
- w_list.append(weights[:, i])
- w_list.append(weights[:, i])
- A = torch.stack(axy_list, dim=1)
- w = torch.stack(w_list, dim=1)
-
- # apply weights
- w_diag = torch.diag_embed(w)
- A = A.transpose(-2, -1) @ w_diag @ A
try:
U, S, V = torch.svd(A)
except:
- return torch.empty(points1_norm.size(0), 3, 3)
+ warnings.warn('SVD did not converge', RuntimeWarning)
+ return torch.empty((points1_norm.size(0), 3, 3), device=points1.device)
+
H = V[..., -1].view(-1, 3, 3)
H = transform2.inverse() @ (H @ transform1)
H_norm = H / (H[..., -1:, -1:] + eps)
diff --git a/test/geometry/test_homography.py b/test/geometry/test_homography.py
index ffe8ed6a7..877bbcb09 100644
--- a/test/geometry/test_homography.py
+++ b/test/geometry/test_homography.py
@@ -28,6 +28,25 @@ class TestFindHomographyDLT:
H = find_homography_dlt(points1, points2, weights)
assert H.shape == (B, 3, 3)
+ @pytest.mark.parametrize("batch_size, num_points", [(1, 4), (2, 5), (3, 6)],)
+ def test_shape_noweights(self, batch_size, num_points, device, dtype):
+ B, N = batch_size, num_points
+ points1 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ points2 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ H = find_homography_dlt(points1, points2, None)
+ assert H.shape == (B, 3, 3)
+
+ @pytest.mark.parametrize("batch_size, num_points", [(1, 4), (2, 5), (3, 6)], )
+ def test_points_noweights(self, batch_size, num_points, device, dtype):
+ B, N = batch_size, num_points
+ points1 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ points2 = torch.rand(B, N, 2, device=device, dtype=dtype)
+ weights = torch.ones(B, N, device=device, dtype=dtype)
+ H_noweights = find_homography_dlt(points1, points2, None)
+ H_withweights = find_homography_dlt(points1, points2, weights)
+ assert H_noweights.shape == (B, 3, 3) and H_withweights.shape == (B, 3, 3)
+ assert_allclose(H_noweights, H_withweights)
+
@pytest.mark.parametrize("batch_size", [1, 2, 5])
def test_clean_points_and_gradcheck(self, batch_size, device):
# generate input data