Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(epipolar): select each batch element's own candidate in motion_from_essential_choose_solution (#5025)

Arij Belmabrouk95ea672cda28
diff --git a/changelog.d/5025.fixed.md b/changelog.d/5025.fixed.md
new file mode 100644
index 000000000..9a2df193b
--- /dev/null
+++ b/changelog.d/5025.fixed.md
@@ -0,0 +1 @@
+`motion_from_essential_choose_solution` now selects each batch element's own best candidate pose independently. Previously, every element in a batched call silently reused the first element's chosen candidate index, which could return a wrong rotation, translation, and triangulated points for any batch (size > 1) where different elements' correct candidates differ.
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index 5ddb4e4b6..bfb9facef 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -770,9 +770,8 @@ def motion_from_essential_choose_solution(
- ``K1`` and ``K2`` are applied inside, so ``x1`` and ``x2`` are pixel coordinates.
- Returns the candidate with the most points at positive depth in both cameras, with
:math:`\|t\| = 1`, and the points triangulated in the first camera's frame at that scale.
- - Known defects: in a batch every element uses the first element's candidate
- (`#2198 <https://github.com/kornia/kornia/issues/2198>`_); with no valid point it returns candidate 0
- without a signal (`#4879 <https://github.com/kornia/kornia/issues/4879>`_).
+ - Known defects: with no valid point it returns candidate 0 without a signal
+ (`#4879 <https://github.com/kornia/kornia/issues/4879>`_).
Args:
E_mat: The essential matrix in the form of :math:`(B, 3, 3)`, or :math:`(3, 3)` with every other input
@@ -851,9 +850,10 @@ def motion_from_essential_choose_solution(
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]
- t_out = ts[:, mask_indices][:, 0, 0]
- points3d_out = X[:, mask_indices][:, 0, 0]
+ batch_idx = torch.arange(mask_indices.shape[0], device=mask_indices.device)
+ R_out = Rs[batch_idx, mask_indices[:, 0]]
+ t_out = ts[batch_idx, mask_indices[:, 0]]
+ points3d_out = X[batch_idx, mask_indices[:, 0]]
if unbatched:
R_out = R_out[0]
diff --git a/tests/geometry/epipolar/test_essential.py b/tests/geometry/epipolar/test_essential.py
index a87b4c454..f455f6dce 100644
--- a/tests/geometry/epipolar/test_essential.py
+++ b/tests/geometry/epipolar/test_essential.py
@@ -1022,7 +1022,7 @@ class TestConventionEssential(BaseTester):
self.assert_close(R2b, R2.expand_as(R2b))
self.assert_close(tb, t.expand_as(tb))
- def test_wart_choose_solution_batched_uses_element0_index_2198(self, device, dtype):
+ def test_convention_choose_solution_batch_independent_2198(self, device, dtype):
two_view = two_view_scene(device, dtype)
_skip_half(dtype, _NO_HALF_LU.format("motion_from_essential_choose_solution"))
K1, K2, x1, x2 = two_view["K1"], two_view["K2"], two_view["x1"], two_view["x2"]
@@ -1037,14 +1037,13 @@ class TestConventionEssential(BaseTester):
for E_in in (E, -E):
R_out, t_out, _ = epi.motion_from_essential_choose_solution(E_in, K1, K2, x1, x2)
assert is_truth(R_out[0], t_out[0])
- # #2198: in the batch [E, -E] with the same correspondences, element 1 gets element 0's index: a wrong pose
- # with points behind a camera.
+ # #2198: each batch element must select its own candidate, independently of the others.
R_b, t_b, X_b = epi.motion_from_essential_choose_solution(
torch.cat([E, -E]), K1.expand(2, 3, 3), K2.expand(2, 3, 3), x1.expand(2, 12, 2), x2.expand(2, 12, 2)
)
assert is_truth(R_b[0], t_b[0])
- assert not is_truth(R_b[1], t_b[1])
- assert (X_b[1, :, 2] < 0).any()
+ assert is_truth(R_b[1], t_b[1])
+ assert (X_b[1, :, 2] > 0).all()
def test_convention_find_essential_no_real_root_nan_4883(self, device, dtype):
_skip_find_essential(device, dtype)