Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Feature] Mask correspondences in motion_from_essential_choose_solution (#816)

Michał Tyszkiewicz727c1f17f36b
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index 211f92495..e8106215f 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -1,5 +1,5 @@
"""Module containing functionalities for the Essential matrix."""
-from typing import Tuple
+from typing import Tuple, Optional
import torch
@@ -134,11 +134,12 @@ def motion_from_essential_choose_solution(
K1: torch.Tensor,
K2: torch.Tensor,
x1: torch.Tensor,
- x2: torch.Tensor
+ x2: torch.Tensor,
+ mask: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
- r"""Recovers the relative camera rotation and the translation from an estimated essential matrix.
+ r"""Recover the relative camera rotation and the translation from an estimated essential matrix.
- The method check the corresponding points in two images and also returns the triangulated
+ The method checks the corresponding points in two images and also returns the triangulated
3d points. Internally uses :py:meth:`~kornia.geometry.epipolar.decompose_essential_matrix` and then chooses
the best solution based on the combination that gives more 3d points in front of the camera plane from
:py:meth:`~kornia.geometry.epipolar.triangulate_points`.
@@ -151,6 +152,10 @@ def motion_from_essential_choose_solution(
coordinates with shape :math:`(*, N, 2)`.
x2 (torch.Tensor): The set of points seen from the first camera frame in the camera plane
coordinates with shape :math:`(*, N, 2)`.
+ mask (torch.Tensor): A boolean mask which can be used to exclude some points from choosing
+ the best solution. This is useful for using this function with sets of points of
+ different cardinality (for instance after filtering with RANSAC) while keeping batch
+ semantics. Mask is of shape :math:`(*, N)`.
Returns:
Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: The rotation and translation plus the
@@ -163,6 +168,9 @@ def motion_from_essential_choose_solution(
assert len(x1.shape) >= 2 and x1.shape[-1] == 2, x1.shape
assert len(x2.shape) >= 2 and x2.shape[-1] == 2, x2.shape
assert len(E_mat.shape[:-2]) == len(K1.shape[:-2]) == len(K2.shape[:-2])
+ if mask is not None:
+ assert len(mask.shape) >= 2, mask.shape
+ assert mask.shape == x1.shape[:-1], mask.shape
# compute four possible pose solutions
Rs, ts = motion_from_essential(E_mat)
@@ -193,8 +201,11 @@ def motion_from_essential_choose_solution(
d2 = projection.depth(R2, t2, X)
# verify the point values that have a postive depth value
- mask = ((d1 > 0.) & (d2 > 0.))
- mask_indices = torch.max(mask.sum(-1), dim=-1, keepdim=True)[1]
+ depth_mask = ((d1 > 0.) & (d2 > 0.))
+ if mask is not None:
+ depth_mask &= mask.unsqueeze(1)
+
+ mask_indices = torch.max(depth_mask.sum(-1), dim=-1, keepdim=True)[1]
# get pose and points 3d and return
R_out = Rs[:, mask_indices][:, 0, 0]
diff --git a/test/geometry/epipolar/test_essential.py b/test/geometry/epipolar/test_essential.py
index 261a7cdc4..9e1544d5e 100644
--- a/test/geometry/epipolar/test_essential.py
+++ b/test/geometry/epipolar/test_essential.py
@@ -292,6 +292,23 @@ class TestMotionFromEssentialChooseSolution:
assert t.shape == (B, 3, 1)
assert X.shape == (B, N, 3)
+ def test_masking(self, device, dtype):
+ E_mat = torch.rand(2, 3, 3, device=device, dtype=dtype)
+ K1 = torch.rand(2, 3, 3, device=device, dtype=dtype)
+ K2 = torch.rand(2, 3, 3, device=device, dtype=dtype)
+ x1 = torch.rand(2, 10, 2, device=device, dtype=dtype)
+ x2 = torch.rand(2, 10, 2, device=device, dtype=dtype)
+
+ R, t, X = epi.motion_from_essential_choose_solution(E_mat, K1, K2, x1[:, 1:-1, :], x2[:, 1:-1, :])
+
+ mask = torch.zeros(2, 10, dtype=torch.bool, device=device)
+ mask[:, 1:-1] = True
+ Rm, tm, Xm = epi.motion_from_essential_choose_solution(E_mat, K1, K2, x1, x2, mask=mask)
+
+ assert_allclose(R, Rm)
+ assert_allclose(t, tm)
+ assert_allclose(X, Xm[:, 1:-1, :])
+
def test_two_view(self, device, dtype):
scene = utils.generate_two_view_random_scene(device, dtype)