projection_z1.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 for the projection of points in the canonical z=1 plane."""
  18. # inspired by: https://github.com/farm-ng/sophus-rs/blob/main/src/sensor/perspective_camera.rs
  19. from __future__ import annotations
  20. from typing import Optional
  21. import kornia.core as ops
  22. from kornia.core import Tensor
  23. from kornia.core.check import KORNIA_CHECK_SHAPE
  24. def project_points_z1(points_in_camera: Tensor) -> Tensor:
  25. r"""Project one or more points from the camera frame into the canonical z=1 plane through perspective division.
  26. .. math::
  27. \begin{bmatrix} u \\ v \\ w \end{bmatrix} =
  28. \begin{bmatrix} x \\ y \\ z \end{bmatrix} / z
  29. .. note::
  30. This function has a precondition that the points are in front of the camera, i.e. z > 0.
  31. If this is not the case, the points will be projected to the canonical plane, but the resulting
  32. points will be behind the camera and causing numerical issues for z == 0.
  33. Args:
  34. points_in_camera: Tensor representing the points to project with shape (..., 3).
  35. Returns:
  36. Tensor representing the projected points with shape (..., 2).
  37. Example:
  38. >>> points = torch.tensor([1., 2., 3.])
  39. >>> project_points_z1(points)
  40. tensor([0.3333, 0.6667])
  41. """
  42. KORNIA_CHECK_SHAPE(points_in_camera, ["*", "3"])
  43. return points_in_camera[..., :2] / points_in_camera[..., 2:3]
  44. def unproject_points_z1(points_in_cam_canonical: Tensor, extension: Optional[Tensor] = None) -> Tensor:
  45. r"""Unproject one or more points from the canonical z=1 plane into the camera frame.
  46. .. math::
  47. \begin{bmatrix} x \\ y \\ z \end{bmatrix} =
  48. \begin{bmatrix} u \\ v \end{bmatrix} \cdot w
  49. Args:
  50. points_in_cam_canonical: Tensor representing the points to unproject with shape (..., 2).
  51. extension: Tensor representing the extension (depth) of the points to unproject with shape (..., 1).
  52. Returns:
  53. Tensor representing the unprojected points with shape (..., 3).
  54. Example:
  55. >>> points = torch.tensor([1., 2.])
  56. >>> extension = torch.tensor([3.])
  57. >>> unproject_points_z1(points, extension)
  58. tensor([3., 6., 3.])
  59. """
  60. KORNIA_CHECK_SHAPE(points_in_cam_canonical, ["*", "2"])
  61. if extension is None:
  62. extension = ops.ones(
  63. points_in_cam_canonical.shape[:-1] + (1,),
  64. device=points_in_cam_canonical.device,
  65. dtype=points_in_cam_canonical.dtype,
  66. ) # (..., 1)
  67. elif extension.shape[0] > 1:
  68. extension = extension[..., None] # (..., 1)
  69. return ops.concatenate([points_in_cam_canonical * extension, extension], dim=-1)
  70. def dx_project_points_z1(points_in_camera: Tensor) -> Tensor:
  71. r"""Compute the derivative of the x projection with respect to the x coordinate.
  72. Returns point derivative of inverse depth point projection with respect to the x coordinate.
  73. .. math::
  74. \frac{\partial \pi}{\partial x} =
  75. \begin{bmatrix}
  76. \frac{1}{z} & 0 & -\frac{x}{z^2} \\
  77. 0 & \frac{1}{z} & -\frac{y}{z^2}
  78. \end{bmatrix}
  79. .. note::
  80. This function has a precondition that the points are in front of the camera, i.e. z > 0.
  81. If this is not the case, the points will be projected to the canonical plane, but the resulting
  82. points will be behind the camera and causing numerical issues for z == 0.
  83. Args:
  84. points_in_camera: Tensor representing the points to project with shape (..., 3).
  85. Returns:
  86. Tensor representing the derivative of the x projection with respect to the x coordinate with shape (..., 2, 3).
  87. Example:
  88. >>> points = torch.tensor([1., 2., 3.])
  89. >>> dx_project_points_z1(points)
  90. tensor([[ 0.3333, 0.0000, -0.1111],
  91. [ 0.0000, 0.3333, -0.2222]])
  92. """
  93. KORNIA_CHECK_SHAPE(points_in_camera, ["*", "3"])
  94. x = points_in_camera[..., 0]
  95. y = points_in_camera[..., 1]
  96. z = points_in_camera[..., 2]
  97. z_inv = 1.0 / z
  98. z_sq = z_inv * z_inv
  99. zeros = ops.zeros_like(z_inv)
  100. return ops.stack(
  101. [
  102. ops.stack([z_inv, zeros, -x * z_sq], dim=-1),
  103. ops.stack([zeros, z_inv, -y * z_sq], dim=-1),
  104. ],
  105. dim=-2,
  106. )