Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Mask with unbatched motion from essential choose solution (#998)

Michał Tyszkiewicz63b59440f296
diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.py
index 5499a87a9..9dab30443 100644
--- a/kornia/geometry/epipolar/essential.py
+++ b/kornia/geometry/epipolar/essential.py
@@ -169,9 +169,22 @@ def motion_from_essential_choose_solution(
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 len(mask.shape) >= 1, mask.shape
assert mask.shape == x1.shape[:-1], mask.shape
+ unbatched = len(E_mat.shape) == 2
+
+ if unbatched:
+ # add a leading batch dimension. We will remove it at the end, before
+ # returning the results
+ E_mat = E_mat[None]
+ K1 = K1[None]
+ K2 = K2[None]
+ x1 = x1[None]
+ x2 = x2[None]
+ if mask is not None:
+ mask = mask[None]
+
# compute four possible pose solutions
Rs, ts = motion_from_essential(E_mat)
@@ -212,6 +225,11 @@ def motion_from_essential_choose_solution(
t_out = ts[:, mask_indices][:, 0, 0]
points3d_out = X[:, mask_indices][:, 0, 0]
+ if unbatched:
+ R_out = R_out[0]
+ t_out = t_out[0]
+ points3d_out = points3d_out[0]
+
return R_out, t_out, points3d_out
diff --git a/setup.py b/setup.py
index 473085946..1c023fb2a 100644
--- a/setup.py
+++ b/setup.py
@@ -83,7 +83,7 @@ def read(*names, **kwargs):
# open readme file and set long description
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
-
+
class clean(distutils.command.clean.clean):
def run(self):
diff --git a/test/geometry/epipolar/test_essential.py b/test/geometry/epipolar/test_essential.py
index 9e1544d5e..6fe76b419 100644
--- a/test/geometry/epipolar/test_essential.py
+++ b/test/geometry/epipolar/test_essential.py
@@ -309,6 +309,28 @@ class TestMotionFromEssentialChooseSolution:
assert_allclose(t, tm)
assert_allclose(X, Xm[:, 1:-1, :])
+ @pytest.mark.parametrize("num_points", [10, 15, 20])
+ def test_unbatched(self, num_points, device, dtype):
+ N = num_points
+ E_mat = torch.rand(3, 3, device=device, dtype=dtype)
+ K1 = torch.rand(3, 3, device=device, dtype=dtype)
+ K2 = torch.rand(3, 3, device=device, dtype=dtype)
+ x1 = torch.rand(N, 2, device=device, dtype=dtype)
+ x2 = torch.rand(N, 2, device=device, dtype=dtype)
+
+ R, t, X = epi.motion_from_essential_choose_solution(E_mat, K1, K2, x1[1:-1, :], x2[1:-1, :])
+ assert R.shape == (3, 3)
+ assert t.shape == (3, 1)
+ assert X.shape == (N - 2, 3)
+
+ mask = torch.zeros(N, 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)