π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(epipolar): normalise each rotation per matrix in decompose_essential_matrix_no_svd (#4915)
Mohansree Vijayakumard5a37b3c35af
diff --git a/changelog.d/4915.fixed.md b/changelog.d/4915.fixed.mdnew file mode 100644index 000000000..8b6d78dca--- /dev/null+++ b/changelog.d/4915.fixed.md@@ -0,0 +1,4 @@+`decompose_essential_matrix_no_svd` now returns rotations for every element of a batch. It used to divide each+rotation by `b Β· b` summed over the whole batch, so each rotation of a batch of two or more came back scaled by its+matrix's share of the batch's squared Frobenius norm: by `1 / B` when all `B` matrices have the same norm, such as the+unit-norm candidates of `find_essential`. A batch of one is unchanged bit for bit. (#4915)diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.pyindex 81c6fab27..100cff7e4 100644--- a/kornia/geometry/epipolar/essential.py+++ b/kornia/geometry/epipolar/essential.py@@ -624,8 +624,6 @@ def decompose_essential_matrix_no_svd(E_mat: torch.Tensor) -> Tuple[torch.TensorConvention:- Same candidate set as :func:`decompose_essential_matrix`, with ``t`` of unit norm.- - Known defects: the rotations are wrong for a batch of more than one matrix- (`#4880 <https://github.com/kornia/kornia/issues/4880>`_).Args:E_mat: The essential matrix in the form of :math:`(*, 3, 3)`.@@ -683,8 +681,8 @@ def decompose_essential_matrix_no_svd(E_mat: torch.Tensor) -> Tuple[torch.Tensor# Eq.24, recover R# (bb)R = Cofactors(E)^T - BE- R1 = (matrix_cofactor_tensor(E_mat) - B1 @ E_mat) / (b1 * b1).sum().unsqueeze(-1)- R2 = (matrix_cofactor_tensor(E_mat) - B2 @ E_mat) / (b2 * b2).sum().unsqueeze(-1)+ R1 = (matrix_cofactor_tensor(E_mat) - B1 @ E_mat) / (b1 * b1).sum(-1)[:, None, None]+ R2 = (matrix_cofactor_tensor(E_mat) - B2 @ E_mat) / (b2 * b2).sum(-1)[:, None, None]return (R1, R2, b1_.unsqueeze(-1))diff --git a/tests/geometry/epipolar/test_essential.py b/tests/geometry/epipolar/test_essential.pyindex 273fc69a1..82e698274 100644--- a/tests/geometry/epipolar/test_essential.py+++ b/tests/geometry/epipolar/test_essential.py@@ -569,8 +569,8 @@ class TestDecomposeEssentialMatrixNoSVD(BaseTester):epi.essential_from_Rt(R1_1, t_1, R2_1, -t_1), epi.essential_from_Rt(R1, t, R2, -t), rtol=1e-3, atol=1e-3)- @pytest.mark.xfail(reason="skip the tests where there are no solutions.")def test_consistency(self, device, dtype):+ _skip_half(dtype, _NO_HALF_LU.format("decompose_essential_matrix"))scene = generate_two_view_random_scene(device, dtype)R1, t1 = scene["R1"], scene["t1"]@@ -998,10 +998,10 @@ class TestConventionEssential(BaseTester):self.assert_close(R_one, R, rtol=1e-4, atol=1e-4)self.assert_close(t_one, t_unit, rtol=1e-4, atol=1e-4)- def test_wart_decompose_no_svd_batch_non_rotations_4880(self, device, dtype):+ def test_decompose_no_svd_batch_matches_single_4880(self, device, dtype):two_view = two_view_scene(device, dtype)- # #4880: the rotation normaliser sums over the whole batch, so a batch of two copies of E returns non-rotations,- # while the same E alone returns rotations.+ # #4880 (fixed): the rotation normaliser used to sum over the whole batch, so a batch of two copies of E+ # returned non-rotations. Each element is now normalised on its own and matches the unbatched result.E = _gt_essential(two_view)eye = torch.eye(3, device=device, dtype=torch.float32)@@ -1009,10 +1009,14 @@ class TestConventionEssential(BaseTester):Rm = Rm.float()return (Rm @ Rm.transpose(-2, -1) - eye).norm(dim=(-2, -1))- R1, R2, _ = epi.decompose_essential_matrix_no_svd(E)+ R1, R2, t = epi.decompose_essential_matrix_no_svd(E)assert orthogonality_error(R1).max() < 0.25 and orthogonality_error(R2).max() < 0.25- R1b, R2b, _ = epi.decompose_essential_matrix_no_svd(torch.cat([E, E]))- assert (orthogonality_error(R1b) > 1.0).all() and (orthogonality_error(R2b) > 1.0).all()+ for B in (2, 3):+ R1b, R2b, tb = epi.decompose_essential_matrix_no_svd(E.repeat(B, 1, 1))+ assert orthogonality_error(R1b).max() < 0.25 and orthogonality_error(R2b).max() < 0.25+ self.assert_close(R1b, R1.expand_as(R1b))+ 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):two_view = two_view_scene(device, dtype)