π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(epipolar): keep the leading dims of a (3, 3) input in decompose_essential_matrix and motion_from_essential (#4998)
εΊζ’η696ad1387b33
diff --git a/changelog.d/4998.breaking.md b/changelog.d/4998.breaking.mdnew file mode 100644index 000000000..111fd965f--- /dev/null+++ b/changelog.d/4998.breaking.md@@ -0,0 +1,4 @@+`decompose_essential_matrix` and `motion_from_essential` keep the leading dims of the input for an unbatched+`(3, 3)` essential matrix: the rotations are now `(3, 3)` and the four candidates `(4, 3, 3)`, matching `t` of shape+`(3, 1)` and `(4, 3, 1)`. They used to come back as `(1, 3, 3)` and `(1, 4, 3, 3)`, so a caller that indexed `R1[0]`+after an unbatched call has to drop the index. Batched inputs are unchanged. Fixes #4878.diff --git a/kornia/geometry/epipolar/essential.py b/kornia/geometry/epipolar/essential.pyindex bde21cfcb..5ddb4e4b6 100644--- a/kornia/geometry/epipolar/essential.py+++ b/kornia/geometry/epipolar/essential.py@@ -578,15 +578,13 @@ def decompose_essential_matrix(E_mat: torch.Tensor) -> Tuple[torch.Tensor, torchpose is not fixed: it can change when ``E_mat`` is negated or rescaled. Select by cheirality with:func:`motion_from_essential_choose_solution`; :ref:`two-view geometry <two-view-conventions>`compares OpenCV's labels.- - Known defects: a ``(3, 3)`` input returns rotations of shape ``(1, 3, 3)`` but ``t`` of shape- ``(3, 1)`` (`#4878 <https://github.com/kornia/kornia/issues/4878>`_).Args:E_mat: The essential matrix in the form of :math:`(*, 3, 3)`.Returns:A tuple containing the first and second possible rotation matrices and the translation vector,- with shapes :math:`[(*, 3, 3), (*, 3, 3), (*, 3, 1)]` for an input with a batch dimension.+ with shapes :math:`[(*, 3, 3), (*, 3, 3), (*, 3, 1)]`: the same leading dims as the input."""KORNIA_CHECK_SHAPE(E_mat, ["*", "3", "3"])@@ -604,8 +602,8 @@ def decompose_essential_matrix(E_mat: torch.Tensor) -> Tuple[torch.Tensor, torchU = torch.where((torch.det(U) < 0.0)[..., None, None], U * mask, U)Vt = torch.where((torch.det(Vt) < 0.0)[..., None, None], Vt * maskt, Vt)- W = cross_product_matrix(torch.tensor([[0.0, 0.0, 1.0]]).type_as(E_mat))- W[..., 2, 2] += 1.0+ # W = [e_z]_x + diag(0, 0, 1), built unbatched so that it does not add a batch dim to an unbatched U.+ W = torch.tensor([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]], device=E_mat.device, dtype=E_mat.dtype)# reconstruct rotations and retrieve translation vectorU_W_Vt = U @ W @ Vt@@ -733,8 +731,6 @@ def motion_from_essential(E_mat: torch.Tensor) -> Tuple[torch.Tensor, torch.TensConvention:- The candidates of :func:`decompose_essential_matrix` are stacked on dim ``-3`` in that order; whichindex is the true pose is not fixed.- - Known defects: a ``(3, 3)`` input returns rotations of shape ``(1, 4, 3, 3)`` but translations of- shape ``(4, 3, 1)`` (`#4878 <https://github.com/kornia/kornia/issues/4878>`_).Args:E_mat: The essential matrix in the form of :math:`(*, 3, 3)`.diff --git a/tests/geometry/epipolar/test_essential.py b/tests/geometry/epipolar/test_essential.pyindex 7a6899df8..a87b4c454 100644--- a/tests/geometry/epipolar/test_essential.py+++ b/tests/geometry/epipolar/test_essential.py@@ -965,17 +965,22 @@ class TestConventionEssential(BaseTester):assert best(E_ones) > 1e3 * best(E_clean)- def test_wart_decompose_unbatched_t_shape_4878(self, device, dtype):+ def test_decompose_unbatched_keeps_input_dims_4878(self, device, dtype):two_view = two_view_scene(device, dtype)_skip_half(dtype, _NO_HALF_LU.format("decompose_essential_matrix"))- # #4878: an unbatched (3, 3) input gains a batch dim on the rotations but not on t.- E = _gt_essential(two_view)[0]- R1, R2, t = epi.decompose_essential_matrix(E)- assert R1.shape == (1, 3, 3) and R2.shape == (1, 3, 3)- assert t.shape == (3, 1)- Rs, ts = epi.motion_from_essential(E)- assert Rs.shape == (1, 4, 3, 3)- assert ts.shape == (4, 3, 1)+ # #4878 (fixed): the constant W was built with a batch dim, so a (3, 3) input returned rotations of shape+ # (1, 3, 3) next to t of shape (3, 1). All outputs now keep the leading dims of the input.+ E = _gt_essential(two_view)+ R1, R2, t = epi.decompose_essential_matrix(E[0])+ assert R1.shape == (3, 3) and R2.shape == (3, 3) and t.shape == (3, 1)+ Rs, ts = epi.motion_from_essential(E[0])+ assert Rs.shape == (4, 3, 3) and ts.shape == (4, 3, 1)+ R1b, R2b, tb = epi.decompose_essential_matrix(E)+ self.assert_close(R1, R1b[0])+ self.assert_close(R2, R2b[0])+ self.assert_close(t, tb[0])+ Rsb, tsb = epi.motion_from_essential(E.expand(2, 1, 3, 3))+ assert Rsb.shape == (2, 1, 4, 3, 3) and tsb.shape == (2, 1, 4, 3, 1)def test_wart_choose_solution_all_masked_returns_candidate0_4879(self, device, dtype):two_view = two_view_scene(device, dtype)