triangulation.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Module with the functionalities for triangulation."""
  18. import torch
  19. from kornia.core import zeros
  20. from kornia.core.check import KORNIA_CHECK_SHAPE
  21. from kornia.geometry.conversions import convert_points_from_homogeneous
  22. from kornia.utils.helpers import _torch_svd_cast
  23. # https://github.com/opencv/opencv_contrib/blob/master/modules/sfm/src/triangulation.cpp#L68
  24. def triangulate_points(
  25. P1: torch.Tensor, P2: torch.Tensor, points1: torch.Tensor, points2: torch.Tensor
  26. ) -> torch.Tensor:
  27. r"""Reconstructs a bunch of points by triangulation.
  28. Triangulates the 3d position of 2d correspondences between several images.
  29. Reference: Internally it uses DLT method from Hartley/Zisserman 12.2 pag.312
  30. The input points are assumed to be in homogeneous coordinate system and being inliers
  31. correspondences. The method does not perform any robust estimation.
  32. Args:
  33. P1: The projection matrix for the first camera with shape :math:`(*, 3, 4)`.
  34. P2: The projection matrix for the second camera with shape :math:`(*, 3, 4)`.
  35. points1: The set of points seen from the first camera frame in the camera plane
  36. coordinates with shape :math:`(*, N, 2)`.
  37. points2: The set of points seen from the second camera frame in the camera plane
  38. coordinates with shape :math:`(*, N, 2)`.
  39. Returns:
  40. The reconstructed 3d points in the world frame with shape :math:`(*, N, 3)`.
  41. """
  42. KORNIA_CHECK_SHAPE(P1, ["*", "3", "4"])
  43. KORNIA_CHECK_SHAPE(P2, ["*", "3", "4"])
  44. KORNIA_CHECK_SHAPE(points1, ["*", "N", "2"])
  45. KORNIA_CHECK_SHAPE(points2, ["*", "N", "2"])
  46. # allocate and construct the equations matrix with shape (*, 4, 4)
  47. points_shape = max(points1.shape, points2.shape) # this allows broadcasting
  48. X = zeros(points_shape[:-1] + (4, 4)).type_as(points1)
  49. for i in range(4):
  50. X[..., 0, i] = points1[..., 0] * P1[..., 2:3, i] - P1[..., 0:1, i]
  51. X[..., 1, i] = points1[..., 1] * P1[..., 2:3, i] - P1[..., 1:2, i]
  52. X[..., 2, i] = points2[..., 0] * P2[..., 2:3, i] - P2[..., 0:1, i]
  53. X[..., 3, i] = points2[..., 1] * P2[..., 2:3, i] - P2[..., 1:2, i]
  54. # 1. Solve the system Ax=0 with smallest eigenvalue
  55. # 2. Return homogeneous coordinates
  56. _, _, V = _torch_svd_cast(X)
  57. points3d_h = V[..., -1]
  58. points3d: torch.Tensor = convert_points_from_homogeneous(points3d_h)
  59. return points3d