π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[Feat] Added decompose 3x4projection matrix (#650)
Chetan Patil8a5952e0f994
diff --git a/kornia/geometry/epipolar/projection.py b/kornia/geometry/epipolar/projection.pyindex 1c2fe051f..99b569413 100644--- a/kornia/geometry/epipolar/projection.py+++ b/kornia/geometry/epipolar/projection.py@@ -1,5 +1,5 @@"""Module for image projections."""-from typing import Union+from typing import Union, Tupleimport torch@@ -108,6 +108,44 @@ def projection_from_KRt(K: torch.Tensor, R: torch.Tensor, t: torch.Tensor) -> toreturn K @ Rt+def KRt_from_projection(P: torch.Tensor, eps: float = 1e-6) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:+ r"""This function decomposes the Projection matrix into Camera-Matrix, Rotation Matrix and Translation vector.++ Args:+ P (torch.Tensor): the projection matrix with shape :math:`(B, 3, 4)`.++ Returns:+ Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:+ - The Camera matrix with shape :math:`(B, 3, 3)`.+ - The Rotation matrix with shape :math:`(B, 3, 3)`.+ - The Translation vector with shape :math:`(B, 3)`.++ """+ assert P.shape[-2:] == (3, 4), "P must be of shape [B, 3, 4]"+ assert len(P.shape) == 3++ submat_3x3 = P[:, 0:3, 0:3]+ last_column = P[:, 0:3, 3].unsqueeze(-1)++ # Trick to turn QR-decomposition into RQ-decomposition+ reverse = torch.tensor([[0, 0, 1], [0, 1, 0], [1, 0, 0]], device=P.device, dtype=P.dtype).unsqueeze(0)+ submat_3x3 = torch.matmul(reverse, submat_3x3).permute(0, 2, 1)+ ortho_mat, upper_mat = torch.qr(submat_3x3)+ ortho_mat = torch.matmul(reverse, ortho_mat.permute(0, 2, 1))+ upper_mat = torch.matmul(reverse, torch.matmul(upper_mat.permute(0, 2, 1), reverse))++ # Turning the `upper_mat's` diagonal elements to positive.+ diagonals = torch.diagonal(upper_mat, dim1=-2, dim2=-1) + eps+ signs = torch.sign(diagonals)+ signs_mat = torch.diag_embed(signs)++ K = torch.matmul(upper_mat, signs_mat)+ R = torch.matmul(signs_mat, ortho_mat)+ t = torch.matmul(torch.inverse(K), last_column)++ return K, R, t++def depth(R: torch.Tensor, t: torch.Tensor, X: torch.Tensor) -> torch.Tensor:r"""Returns the depth of a point transformed by a rigid transform.diff --git a/test/geometry/epipolar/test_projection.py b/test/geometry/epipolar/test_projection.pyindex bdb74eead..51d534f17 100644--- a/test/geometry/epipolar/test_projection.py+++ b/test/geometry/epipolar/test_projection.py@@ -113,6 +113,34 @@ class TestProjectionFromKRt:P_estimated = epi.projection_from_KRt(K, R, t)assert_allclose(P_estimated, P_expected)+ def test_krt_from_projection(self, device, dtype):+ P = torch.tensor([[+ [10., 0., 30., 100.],+ [0., 20., 40., 160.],+ [0., 0., 1., 3.],+ ]], device=device, dtype=dtype)++ K_expected = torch.tensor([[+ [10., 0., 30.],+ [0., 20., 40.],+ [0., 0., 1.],+ ]], device=device, dtype=dtype)++ R_expected = torch.tensor([[+ [1., 0., 0.],+ [0., 1., 0.],+ [0., 0., 1.],+ ]], device=device, dtype=dtype)++ t_expected = torch.tensor([+ [[1.], [2.], [3.]]+ ], device=device, dtype=dtype)++ K_estimated, R_estimated, t_estimated = epi.KRt_from_projection(P)+ assert_allclose(K_estimated, K_expected)+ assert_allclose(R_estimated, R_expected)+ assert_allclose(t_estimated, t_expected)+def test_gradcheck(self, device):K = torch.rand(1, 3, 3, device=device, dtype=torch.float64, requires_grad=True)R = torch.rand(1, 3, 3, device=device, dtype=torch.float64)@@ -138,3 +166,81 @@ class TestProjectionsFromFundamental:F_mat = torch.rand(1, 3, 3, device=device, dtype=torch.float64, requires_grad=True)assert gradcheck(epi.projections_from_fundamental,(F_mat,), raise_exception=True)+++class TestKRtFromProjection:+ def test_smoke(self, device, dtype):+ P = torch.randn(1, 3, 4, device=device, dtype=dtype)+ K, R, t = epi.KRt_from_projection(P)+ assert K.shape == (1, 3, 3)+ assert R.shape == (1, 3, 3)+ assert t.shape == (1, 3, 1)++ @pytest.mark.parametrize("batch_size", [1, 2, 4])+ def test_shape(self, batch_size, device, dtype):+ B: int = batch_size+ P = torch.rand(B, 3, 4, device=device, dtype=dtype)+ K, R, t = epi.KRt_from_projection(P)++ assert K.shape == (B, 3, 3)+ assert R.shape == (B, 3, 3)+ assert t.shape == (B, 3, 1)++ def test_simple(self, device, dtype):+ P = torch.tensor([[+ [308., 139., 231., 84.],+ [481., 161., 358., 341.],+ [384., 387., 459., 102.]+ ]], device=device, dtype=dtype)++ K_expected = torch.tensor([[+ [17.006138, 122.441254, 390.211426],+ [0.0, 228.743622, 577.167480],+ [0.0, 0.0, 712.675232]+ ]], device=device, dtype=dtype)++ R_expected = torch.tensor([[+ [0.396559, 0.511023, -0.762625],+ [0.743249, -0.666318, -0.060006],+ [0.538815, 0.543024, 0.644052]+ ]], device=device, dtype=dtype)++ t_expected = torch.tensor([+ [[-6.477699], [1.129624], [0.143123]]+ ], device=device, dtype=dtype)++ K_estimated, R_estimated, t_estimated = epi.KRt_from_projection(P)+ assert_allclose(K_estimated, K_expected)+ assert_allclose(R_estimated, R_expected)+ assert_allclose(t_estimated, t_expected)++ def test_projection_from_krt(self, device, dtype):+ K = torch.tensor([[+ [17.006138, 122.441254, 390.211426],+ [0.0, 228.743622, 577.167480],+ [0.0, 0.0, 712.675232]+ ]], device=device, dtype=dtype)++ R = torch.tensor([[+ [0.396559, 0.511023, -0.762625],+ [0.743249, -0.666318, -0.060006],+ [0.538815, 0.543024, 0.644052]+ ]], device=device, dtype=dtype)++ t = torch.tensor([+ [[-6.477699], [1.129624], [0.143123]],+ ], device=device, dtype=dtype)++ P_expected = torch.tensor([[+ [308., 139., 231., 84.],+ [481., 161., 358., 341.],+ [384., 387., 459., 102.]+ ]], device=device, dtype=dtype)++ P_estimated = epi.projection_from_KRt(K, R, t)+ assert_allclose(P_estimated, P_expected)++ def test_gradcheck(self, device):+ P_mat = torch.rand(1, 3, 4, device=device, dtype=torch.float64, requires_grad=True)+ assert gradcheck(epi.KRt_from_projection,+ (P_mat,), raise_exception=True)