Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add ProjectionZ1, Orthographic, Affine, KannalaBrandt (#2728)

Edgar Riba53fdaca1ba8f
diff --git a/docs/source/geometry.camera.rst b/docs/source/geometry.camera.rst
index d57e51440..f33113230 100644
--- a/docs/source/geometry.camera.rst
+++ b/docs/source/geometry.camera.rst
@@ -3,6 +3,28 @@ kornia.geometry.camera
.. currentmodule:: kornia.geometry.camera
+Projections
+-----------
+
+.. autofunction:: project_points_z1
+.. autofunction:: unproject_points_z1
+.. autofunction:: dx_project_points_z1
+
+.. autofunction:: project_points_orthographic
+.. autofunction:: unproject_points_orthographic
+.. autofunction:: dx_project_points_orthographic
+
+Distortion
+----------
+
+.. autofunction:: distort_points_affine
+.. autofunction:: undistort_points_affine
+.. autofunction:: dx_distort_points_affine
+
+.. autofunction:: distort_points_kannala_brandt
+.. autofunction:: undistort_points_kannala_brandt
+.. autofunction:: dx_distort_points_kannala_brandt
+
.. toctree::
:maxdepth: 2
diff --git a/kornia/geometry/camera/__init__.py b/kornia/geometry/camera/__init__.py
index 343ac3348..e5da112a1 100644
--- a/kornia/geometry/camera/__init__.py
+++ b/kornia/geometry/camera/__init__.py
@@ -1,5 +1,36 @@
+from .distortion_affine import distort_points_affine, dx_distort_points_affine, undistort_points_affine
+from .distortion_kannala_brandt import (
+ distort_points_kannala_brandt,
+ dx_distort_points_kannala_brandt,
+ undistort_points_kannala_brandt,
+)
from .perspective import project_points, unproject_points
from .pinhole import PinholeCamera, cam2pixel, pixel2cam
+from .projection_orthographic import (
+ dx_project_points_orthographic,
+ project_points_orthographic,
+ unproject_points_orthographic,
+)
+from .projection_z1 import dx_project_points_z1, project_points_z1, unproject_points_z1
from .stereo import StereoCamera
-__all__ = ["PinholeCamera", "StereoCamera", "pixel2cam", "cam2pixel", "unproject_points", "project_points"]
+__all__ = [
+ "PinholeCamera",
+ "StereoCamera",
+ "cam2pixel",
+ "dx_distort_points_affine",
+ "dx_distort_points_kannala_brandt",
+ "dx_project_points_orthographic",
+ "dx_project_points_z1",
+ "pixel2cam",
+ "project_points",
+ "project_points_orthographic",
+ "project_points_z1",
+ "unproject_points",
+ "unproject_points_orthographic",
+ "unproject_points_z1",
+ "distort_points_affine",
+ "distort_points_kannala_brandt",
+ "undistort_points_affine",
+ "undistort_points_kannala_brandt",
+]
diff --git a/kornia/geometry/camera/distortion_affine.py b/kornia/geometry/camera/distortion_affine.py
new file mode 100644
index 000000000..0142dbafb
--- /dev/null
+++ b/kornia/geometry/camera/distortion_affine.py
@@ -0,0 +1,110 @@
+"""Module containing the affine distortion model."""
+# inspired by: https://github.com/farm-ng/sophus-rs/blob/main/src/sensor/affine.rs
+import kornia.core as ops
+from kornia.core import Tensor
+from kornia.core.check import KORNIA_CHECK_SHAPE
+
+
+def distort_points_affine(projected_points_in_camera_z1_plane: Tensor, params: Tensor) -> Tensor:
+ r"""Distort one or more points from the canonical z=1 plane into the camera frame.
+
+ .. math::
+ \begin{bmatrix} u \\ v \end{bmatrix} =
+ \begin{bmatrix} f_x & 0 \\ 0 & f_y \end{bmatrix}
+ \begin{bmatrix} x \\ y \end{bmatrix} +
+ \begin{bmatrix} c_x \\ c_y \end{bmatrix}
+
+ Args:
+ projected_points_in_camera_z1_plane: Tensor representing the points to distort with shape (..., 2).
+ params: Tensor representing the parameters of the affine distortion model with shape (..., 4).
+
+ Returns:
+ Tensor representing the distorted points with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([319.5, 239.5]) # center of a 640x480 image
+ >>> params = torch.tensor([600., 600., 319.5, 239.5])
+ >>> distort_points_affine(points, params)
+ tensor([192019.5000, 143939.5000])
+ """
+ KORNIA_CHECK_SHAPE(projected_points_in_camera_z1_plane, ["*", "2"])
+ KORNIA_CHECK_SHAPE(params, ["*", "4"])
+
+ x = projected_points_in_camera_z1_plane[..., 0]
+ y = projected_points_in_camera_z1_plane[..., 1]
+
+ fx, fy = params[..., 0], params[..., 1]
+ cx, cy = params[..., 2], params[..., 3]
+
+ u = fx * x + cx
+ v = fy * y + cy
+
+ return ops.stack([u, v], dim=-1)
+
+
+def undistort_points_affine(distorted_points_in_camera: Tensor, params: Tensor) -> Tensor:
+ r"""Undistort one or more points from the camera frame into the canonical z=1 plane.
+
+ .. math::
+ \begin{bmatrix} x \\ y \end{bmatrix} =
+ \begin{bmatrix} u \\ v \end{bmatrix} -
+ \begin{bmatrix} c_x \\ c_y \end{bmatrix}
+ \begin{bmatrix} f_x & 0 \\ 0 & f_y \end{bmatrix}^{-1}
+
+ Args:
+ distorted_points_in_camera: Tensor representing the points to undistort with shape (..., 2).
+ params: Tensor representing the parameters of the affine distortion model with shape (..., 4).
+
+ Returns:
+ Tensor representing the undistorted points with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([319.5, 239.5]) # center of a 640x480 image
+ >>> params = torch.tensor([600., 600., 319.5, 239.5])
+ >>> undistort_points_affine(points, params)
+ tensor([0., 0.])
+ """
+ KORNIA_CHECK_SHAPE(distorted_points_in_camera, ["*", "2"])
+ KORNIA_CHECK_SHAPE(params, ["*", "4"])
+
+ u = distorted_points_in_camera[..., 0]
+ v = distorted_points_in_camera[..., 1]
+
+ fx, fy = params[..., 0], params[..., 1]
+ cx, cy = params[..., 2], params[..., 3]
+
+ x = (u - cx) / fx
+ y = (v - cy) / fy
+
+ return ops.stack([x, y], dim=-1)
+
+
+def dx_distort_points_affine(projected_points_in_camera_z1_plane: Tensor, params: Tensor) -> Tensor:
+ r"""Compute the derivative of the x distortion with respect to the x coordinate.
+
+ .. math::
+ \frac{\partial u}{\partial x} =
+ \begin{bmatrix} f_x & 0 \\ 0 & f_y \end{bmatrix}
+
+ Args:
+ projected_points_in_camera_z1_plane: Tensor representing the points to distort with shape (..., 2).
+ params: Tensor representing the parameters of the affine distortion model with shape (..., 4).
+
+ Returns:
+ Tensor representing the derivative of the x distortion with respect to the x coordinate with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([319.5, 239.5]) # center of a 640x480 image
+ >>> params = torch.tensor([600., 600., 319.5, 239.5])
+ >>> dx_distort_points_affine(points, params)
+ tensor([[600., 0.],
+ [ 0., 600.]])
+ """
+ KORNIA_CHECK_SHAPE(projected_points_in_camera_z1_plane, ["*", "2"])
+ KORNIA_CHECK_SHAPE(params, ["*", "4"])
+
+ fx, fy = params[..., 0], params[..., 1]
+
+ zeros = ops.zeros_like(fx)
+
+ return ops.stack([ops.stack([fx, zeros], dim=-1), ops.stack([zeros, fy], dim=-1)], dim=-2)
diff --git a/kornia/geometry/camera/distortion_kannala_brandt.py b/kornia/geometry/camera/distortion_kannala_brandt.py
new file mode 100644
index 000000000..323ce07e5
--- /dev/null
+++ b/kornia/geometry/camera/distortion_kannala_brandt.py
@@ -0,0 +1,245 @@
+# inspired by: shttps://github.com/farm-ng/sophus-rs/blob/main/src/sensor/kannala_brandt.rs
+import kornia.core as ops
+from kornia.core import Tensor
+from kornia.core.check import KORNIA_CHECK_SHAPE
+from kornia.geometry.camera.distortion_affine import distort_points_affine
+
+
+def _distort_points_kannala_brandt_impl(
+ projected_points_in_camera_z1_plane: Tensor,
+ params: Tensor,
+) -> Tensor:
+ # https://github.com/farm-ng/sophus-rs/blob/20f6cac68f17fe1ac41d0aa8a27489e2b886806f/src/sensor/kannala_brandt.rs#L51-L67
+ x = projected_points_in_camera_z1_plane[..., 0]
+ y = projected_points_in_camera_z1_plane[..., 1]
+
+ fx, fy = params[..., 0], params[..., 1]
+ cx, cy = params[..., 2], params[..., 3]
+
+ k0 = params[..., 4]
+ k1 = params[..., 5]
+ k2 = params[..., 6]
+ k3 = params[..., 7]
+
+ radius_sq = x**2 + y**2
+
+ radius = radius_sq.sqrt()
+ radius_inverse = 1.0 / radius
+ theta = radius.atan2(ops.ones_like(radius))
+ theta2 = theta**2
+ theta4 = theta2**2
+ theta6 = theta2 * theta4
+ theta8 = theta4**2
+
+ r_distorted = theta * (1.0 + k0 * theta2 + k1 * theta4 + k2 * theta6 + k3 * theta8)
+
+ scaling = r_distorted * radius_inverse
+
+ u = fx * scaling * x + cx
+ v = fy * scaling * y + cy
+
+ return ops.stack([u, v], dim=-1)
+
+
+def distort_points_kannala_brandt(projected_points_in_camera_z1_plane: Tensor, params: Tensor) -> Tensor:
+ r"""Distort one or more points from the canonical z=1 plane into the camera frame using the Kannala-Brandt
+ model.
+
+ Args:
+ projected_points_in_camera_z1_plane: Tensor representing the points to distort with shape (..., 2).
+ params: Tensor representing the parameters of the Kannala-Brandt distortion model with shape (..., 8).
+
+ Returns:
+ Tensor representing the distorted points with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([319.5, 239.5]) # center of a 640x480 image
+ >>> params = torch.tensor([1000.0, 1000.0, 320.0, 280.0, 0.1, 0.01, 0.001, 0.0001])
+ >>> distort_points_kannala_brandt(points, params)
+ tensor([1982.6832, 1526.3619])
+ """
+ KORNIA_CHECK_SHAPE(projected_points_in_camera_z1_plane, ["*", "2"])
+ KORNIA_CHECK_SHAPE(params, ["*", "8"])
+
+ x = projected_points_in_camera_z1_plane[..., 0]
+ y = projected_points_in_camera_z1_plane[..., 1]
+
+ radius_sq = x**2 + y**2
+
+ # TODO: we can optimize this by passing the radius_sq to the impl functions. Check if it's worth it.
+ distorted_points = ops.where(
+ radius_sq[..., None] > 1e-8,
+ _distort_points_kannala_brandt_impl(
+ projected_points_in_camera_z1_plane,
+ params,
+ ),
+ distort_points_affine(projected_points_in_camera_z1_plane, params[..., :4]),
+ )
+
+ return distorted_points
+
+
+def undistort_points_kannala_brandt(distorted_points_in_camera: Tensor, params: Tensor) -> Tensor:
+ r"""Undistort one or more points from the camera frame into the canonical z=1 plane using the Kannala-Brandt
+ model.
+
+ Args:
+ distorted_points_in_camera: Tensor representing the points to undistort with shape (..., 2).
+ params: Tensor representing the parameters of the Kannala-Brandt distortion model with shape (..., 8).
+
+ Returns:
+ Tensor representing the undistorted points with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([319.5, 239.5]) # center of a 640x480 image
+ >>> params = torch.tensor([1000.0, 1000.0, 320.0, 280.0, 0.1, 0.01, 0.001, 0.0001])
+ >>> undistort_points_kannala_brandt(points, params).shape
+ torch.Size([2])
+ """
+ KORNIA_CHECK_SHAPE(distorted_points_in_camera, ["*", "2"])
+ KORNIA_CHECK_SHAPE(params, ["*", "8"])
+
+ x = distorted_points_in_camera[..., 0]
+ y = distorted_points_in_camera[..., 1]
+
+ fx, fy = params[..., 0], params[..., 1]
+ cx, cy = params[..., 2], params[..., 3]
+
+ k0 = params[..., 4]
+ k1 = params[..., 5]
+ k2 = params[..., 6]
+ k3 = params[..., 7]
+
+ un = (x - cx) / fx
+ vn = (y - cy) / fy
+ rth2 = un**2 + vn**2
+
+ # TODO: explore stop condition (won't work with pytorch with batched inputs)
+ # Additionally, with this stop condition we can avoid adding 1e-8 to the denominator
+ # in the return statement of the function.
+
+ # if rth2.abs() < 1e-8:
+ # return distorted_points_in_camera
+
+ rth = rth2.sqrt()
+
+ th = rth.sqrt()
+
+ iters = 0
+
+ # gauss-newton
+
+ while True:
+ th2 = th**2
+ th4 = th2**2
+ th6 = th2 * th4
+ th8 = th4**2
+
+ thd = th * (1.0 + k0 * th2 + k1 * th4 + k2 * th6 + k3 * th8)
+
+ d_thd_wtr_th = 1.0 + 3.0 * k0 * th2 + 5.0 * k1 * th4 + 7.0 * k2 * th6 + 9.0 * k3 * th8
+
+ step = (thd - rth) / d_thd_wtr_th
+ th = th - step
+
+ iters += 1
+
+ # TODO: improve stop condition by masking only the elements that have converged
+ th_abs_mask = th.abs() < 1e-8
+
+ if th_abs_mask.all():
+ break
+
+ if iters >= 20:
+ break
+
+ radius_undistorted = th.tan()
+
+ undistorted_points = ops.where(
+ radius_undistorted[..., None] < 0.0,
+ ops.stack(
+ [
+ -radius_undistorted * un / (rth + 1e-8),
+ -radius_undistorted * vn / (rth + 1e-8),
+ ],
+ dim=-1,
+ ),
+ ops.stack(
+ [
+ radius_undistorted * un / (rth + 1e-8),
+ radius_undistorted * vn / (rth + 1e-8),
+ ],
+ dim=-1,
+ ),
+ )
+
+ return undistorted_points
+
+
+def dx_distort_points_kannala_brandt(projected_points_in_camera_z1_plane: Tensor, params: Tensor) -> Tensor:
+ r"""Compute the derivative of the x distortion with respect to the x coordinate.
+
+ .. math::
+ \frac{\partial u}{\partial x} =
+ \begin{bmatrix} f_x & 0 \\ 0 & f_y \end{bmatrix}
+
+ Args:
+ projected_points_in_camera_z1_plane: Tensor representing the points to distort with shape (..., 2).
+ params: Tensor representing the parameters of the Kannala-Brandt distortion model with shape (..., 8).
+
+ Returns:
+ Tensor representing the derivative of the x distortion with respect to the x coordinate with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([1., 2.])
+ >>> params = torch.tensor([1000.0, 1000.0, 320.0, 280.0, 0.1, 0.01, 0.001, 0.0001])
+ >>> dx_distort_points_kannala_brandt(points, params)
+ tensor([[ 486.0507, -213.5573],
+ [-213.5573, 165.7147]])
+ """
+ KORNIA_CHECK_SHAPE(projected_points_in_camera_z1_plane, ["*", "2"])
+ KORNIA_CHECK_SHAPE(params, ["*", "8"])
+
+ a = projected_points_in_camera_z1_plane[..., 0]
+ b = projected_points_in_camera_z1_plane[..., 1]
+
+ fx, fy = params[..., 0], params[..., 1]
+
+ k0 = params[..., 4]
+ k1 = params[..., 5]
+ k2 = params[..., 6]
+ k3 = params[..., 7]
+
+ # TODO: return identity matrix if a and b are zero
+ # radius_sq = a ** 2 + b ** 2
+
+ c0 = a.pow(2.0)
+ c1 = b.pow(2.0)
+ c2 = c0 + c1
+ c3 = c2.pow(5.0 / 2.0)
+ c4 = c2 + 1.0
+ c5 = c2.sqrt().atan()
+ c6 = c5.pow(2.0)
+ c7 = c6 * k0
+ c8 = c5.pow(4.0)
+ c9 = c8 * k1
+ c10 = c5.pow(6.0)
+ c11 = c10 * k2
+ c12 = c5.pow(8.0) * k3
+ c13 = 1.0 * c4 * c5 * (c11 + c12 + c7 + c9 + 1.0)
+ c14 = c13 * c3
+ c15 = c2.pow(3.0 / 2.0)
+ c16 = c13 * c15
+ c17 = 1.0 * c11 + 1.0 * c12 + 2.0 * c6 * (4.0 * c10 * k3 + 2.0 * c6 * k1 + 3.0 * c8 * k2 + k0)
+ c18 = c17 * c2.pow(2.0)
+ c19 = 1.0 / c4
+ c20 = c19 / c2.pow(3.0)
+ c21 = a * b * c19 * (-c13 * c2 + c15 * c17) / c3
+
+ return ops.stack(
+ [
+ ops.stack([c20 * fx * (-c0 * c16 + c0 * c18 + c14), c21 * fx], dim=-1),
+ ops.stack([c21 * fy, c20 * fy * (-c1 * c16 + c1 * c18 + c14)], dim=-1),
+ ],
+ dim=-2,
+ )
diff --git a/kornia/geometry/camera/projection_orthographic.py b/kornia/geometry/camera/projection_orthographic.py
new file mode 100644
index 000000000..54ef4c150
--- /dev/null
+++ b/kornia/geometry/camera/projection_orthographic.py
@@ -0,0 +1,78 @@
+"""Module containing functions for orthographic projection."""
+# inspired by: https://github.com/farm-ng/sophus-rs/blob/main/src/sensor/ortho_camera.rs
+import kornia.core as ops
+from kornia.core import Tensor
+from kornia.core.check import KORNIA_CHECK_SHAPE
+
+
+def project_points_orthographic(points_in_camera: Tensor) -> Tensor:
+ r"""Project one or more points from the camera frame into the canonical z=1 plane through orthographic
+ projection.
+
+ .. math::
+ \begin{bmatrix} u \\ v \end{bmatrix} =
+ \begin{bmatrix} x \\ y \\ z \end{bmatrix}
+
+
+ Args:
+ points_in_camera: Tensor representing the points to project.
+
+ Returns:
+ Tensor representing the projected points.
+
+ Example:
+ >>> points = torch.tensor([1., 2., 3.])
+ >>> project_points_orthographic(points)
+ tensor([1., 2.])
+ """
+ KORNIA_CHECK_SHAPE(points_in_camera, ["*", "3"])
+ return points_in_camera[..., :2]
+
+
+def unproject_points_orthographic(points_in_camera: Tensor, extension: Tensor) -> Tensor:
+ r"""Unproject one or more points from the canonical z=1 plane into the camera frame.
+
+ .. math::
+ \begin{bmatrix} x \\ y \\ z \end{bmatrix} =
+ \begin{bmatrix} u \\ v \\ w \end{bmatrix}
+
+ Args:
+ points_in_camera: Tensor representing the points to unproject with shape (..., 2).
+ extension: Tensor representing the extension of the points to unproject with shape (..., 1).
+
+ Returns:
+ Tensor representing the unprojected points with shape (..., 3).
+
+ Example:
+ >>> points = torch.tensor([1., 2.])
+ >>> extension = torch.tensor([3.])
+ >>> unproject_points_orthographic(points, extension)
+ tensor([1., 2., 3.])
+ """
+ KORNIA_CHECK_SHAPE(points_in_camera, ["*", "2"])
+
+ if len(points_in_camera.shape) != len(extension.shape):
+ extension = extension[..., None]
+
+ return ops.concatenate([points_in_camera, extension], dim=-1)
+
+
+def dx_project_points_orthographic(points_in_camera: Tensor) -> Tensor:
+ r"""Compute the derivative of the x projection with respect to the x coordinate.
+
+ .. math::
+ \frac{\partial u}{\partial x} = 1
+
+ Args:
+ points_in_camera: Tensor representing the points to project.
+
+ Returns:
+ Tensor representing the derivative of the x projection with respect to the x coordinate.
+
+ Example:
+ >>> points = torch.tensor([1., 2., 3.])
+ >>> dx_project_points_orthographic(points)
+ tensor([1.])
+ """
+ KORNIA_CHECK_SHAPE(points_in_camera, ["*", "3"])
+ return ops.ones_like(points_in_camera[..., 0:1])
diff --git a/kornia/geometry/camera/projection_z1.py b/kornia/geometry/camera/projection_z1.py
new file mode 100644
index 000000000..4a330d32d
--- /dev/null
+++ b/kornia/geometry/camera/projection_z1.py
@@ -0,0 +1,119 @@
+"""Module for the projection of points in the canonical z=1 plane."""
+# inspired by: https://github.com/farm-ng/sophus-rs/blob/main/src/sensor/perspective_camera.rs
+from __future__ import annotations
+
+from typing import Optional
+
+import kornia.core as ops
+from kornia.core import Tensor
+from kornia.core.check import KORNIA_CHECK_SHAPE
+
+
+def project_points_z1(points_in_camera: Tensor) -> Tensor:
+ r"""Project one or more points from the camera frame into the canonical z=1 plane through perspective division.
+
+ .. math::
+
+ \begin{bmatrix} u \\ v \\ w \end{bmatrix} =
+ \begin{bmatrix} x \\ y \\ z \end{bmatrix} / z
+
+ .. note::
+
+ This function has a precondition that the points are in front of the camera, i.e. z > 0.
+ If this is not the case, the points will be projected to the canonical plane, but the resulting
+ points will be behind the camera and causing numerical issues for z == 0.
+
+ Args:
+ points_in_camera: Tensor representing the points to project with shape (..., 3).
+
+ Returns:
+ Tensor representing the projected points with shape (..., 2).
+
+ Example:
+ >>> points = torch.tensor([1., 2., 3.])
+ >>> project_points_z1(points)
+ tensor([0.3333, 0.6667])
+ """
+ KORNIA_CHECK_SHAPE(points_in_camera, ["*", "3"])
+ return points_in_camera[..., :2] / points_in_camera[..., 2:3]
+
+
+def unproject_points_z1(points_in_cam_canonical: Tensor, extension: Optional[Tensor] = None) -> Tensor:
+ r"""Unproject one or more points from the canonical z=1 plane into the camera frame.
+
+ .. math::
+ \begin{bmatrix} x \\ y \\ z \end{bmatrix} =
+ \begin{bmatrix} u \\ v \end{bmatrix} \cdot w
+
+ Args:
+ points_in_cam_canonical: Tensor representing the points to unproject with shape (..., 2).
+ extension: Tensor representing the extension (depth) of the points to unproject with shape (..., 1).
+
+ Returns:
+ Tensor representing the unprojected points with shape (..., 3).
+
+ Example:
+ >>> points = torch.tensor([1., 2.])
+ >>> extension = torch.tensor([3.])
+ >>> unproject_points_z1(points, extension)
+ tensor([3., 6., 3.])
+ """
+ KORNIA_CHECK_SHAPE(points_in_cam_canonical, ["*", "2"])
+
+ if extension is None:
+ extension = ops.ones(
+ points_in_cam_canonical.shape[:-1] + (1,),
+ device=points_in_cam_canonical.device,
+ dtype=points_in_cam_canonical.dtype,
+ ) # (..., 1)
+ elif extension.shape[0] > 1:
+ extension = extension[..., None] # (..., 1)
+
+ return ops.concatenate([points_in_cam_canonical * extension, extension], dim=-1)
+
+
+def dx_project_points_z1(points_in_camera: Tensor) -> Tensor:
+ r"""Compute the derivative of the x projection with respect to the x coordinate.
+
+ Returns point derivative of inverse depth point projection with respect to the x coordinate.
+
+ .. math::
+ \frac{\partial \pi}{\partial x} =
+ \begin{bmatrix}
+ \frac{1}{z} & 0 & -\frac{x}{z^2} \\
+ 0 & \frac{1}{z} & -\frac{y}{z^2}
+ \end{bmatrix}
+
+ .. note::
+ This function has a precondition that the points are in front of the camera, i.e. z > 0.
+ If this is not the case, the points will be projected to the canonical plane, but the resulting
+ points will be behind the camera and causing numerical issues for z == 0.
+
+ Args:
+ points_in_camera: Tensor representing the points to project with shape (..., 3).
+
+ Returns:
+ Tensor representing the derivative of the x projection with respect to the x coordinate with shape (..., 2, 3).
+
+ Example:
+ >>> points = torch.tensor([1., 2., 3.])
+ >>> dx_project_points_z1(points)
+ tensor([[ 0.3333, 0.0000, -0.1111],
+ [ 0.0000, 0.3333, -0.2222]])
+ """
+ KORNIA_CHECK_SHAPE(points_in_camera, ["*", "3"])
+
+ x = points_in_camera[..., 0]
+ y = points_in_camera[..., 1]
+ z = points_in_camera[..., 2]
+
+ z_inv = 1.0 / z
+ z_sq = z_inv * z_inv
+ zeros = ops.zeros_like(z_inv)
+ return ops.stack(
+ [
+ ops.stack([z_inv, zeros, -x * z_sq], dim=-1),
+ ops.stack([zeros, z_inv, -y * z_sq], dim=-1),
+ ],
+ dim=-2,
+ )
diff --git a/kornia/sensors/camera/projection_model.py b/kornia/sensors/camera/projection_model.py
index ee9840ace..a7850cc06 100644
--- a/kornia/sensors/camera/projection_model.py
+++ b/kornia/sensors/camera/projection_model.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from kornia.core import Tensor, diag
+from kornia.core import Tensor
from kornia.geometry.vector import Vector2, Vector3
@@ -23,7 +23,7 @@ class Z1Projection:
"""
xy = points.data[..., :2]
z = points.z
- uv = (xy.T @ diag(z).inverse()).T if len(z.shape) else xy.T * 1 / z
+ uv = (xy.T @ z.diag().inverse()).T if len(z.shape) else xy.T * 1 / z
return Vector2(uv)
def unproject(self, points: Vector2, depth: Tensor | float) -> Vector3:
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index d1eb5eb45..805b73adc 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -113,12 +113,11 @@ class BaseTester(ABC):
def test_gradcheck(self, device: Device) -> None:
raise NotImplementedError("Implement a stupid routine.")
- @abstractmethod
def test_module(self, device: Device, dtype: Dtype) -> None:
- raise NotImplementedError("Implement a stupid routine.")
+ pass
+ @staticmethod
def assert_close(
- self,
actual: Tensor,
expected: Tensor,
rtol: Optional[float] = None,
diff --git a/pyproject.toml b/pyproject.toml
index 5da520dc9..0acfa13b2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -163,7 +163,7 @@ requires = [
"docs/*" = ["S101", "PLR0912", "PLR0915"] # allow assert, ignore max branches and statements
"docs/generate_examples.py" = ["C901"] # Allow too complex function
"kornia/__init__.py" = ["I001"] # Allow unsorted imports
- "test/*" = ["S101", "S311", "BLE", "RUF012"] # allow assert, random, ignore BLE, mutable class attr
+ "test/*" = ["S101", "S311", "BLE", "RUF012", "RUF005"] # allow assert, random, ignore BLE, mutable class attr
[tool.pytest.ini_options]
addopts = "--color=yes"
diff --git a/test/geometry/camera/test_distortion.py b/test/geometry/camera/test_distortion.py
new file mode 100644
index 000000000..bb6c23b67
--- /dev/null
+++ b/test/geometry/camera/test_distortion.py
@@ -0,0 +1,243 @@
+import pytest
+import torch
+
+from kornia.geometry.camera.distortion_affine import (
+ distort_points_affine,
+ dx_distort_points_affine,
+ undistort_points_affine,
+)
+from kornia.geometry.camera.distortion_kannala_brandt import (
+ distort_points_kannala_brandt,
+ dx_distort_points_kannala_brandt,
+ undistort_points_kannala_brandt,
+)
+from kornia.testing import BaseTester, tensor_to_gradcheck_var
+
+
+class TestDistortionAffine(BaseTester):
+ def test_smoke(self, device, dtype):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5], device=device, dtype=dtype)
+ assert distort_points_affine(points, params) is not None
+
+ def _test_cardinality_distort_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (2,), device=device, dtype=dtype)
+ params = torch.rand(batch_tuple + (4,), device=device, dtype=dtype)
+ assert distort_points_affine(points, params).shape == batch_tuple + (2,)
+
+ def _test_cardinality_undistort_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (2,), device=device, dtype=dtype)
+ params = torch.rand(batch_tuple + (4,), device=device, dtype=dtype)
+ assert undistort_points_affine(points, params).shape == batch_tuple + (2,)
+
+ @pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
+ def test_cardinality(self, device, dtype, batch_size):
+ self._test_cardinality_distort_batch(device, dtype, batch_size)
+ self._test_cardinality_undistort_batch(device, dtype, batch_size)
+
+ # NOTE: data generated with sophus-rs
+ def test_distort_points_roundtrip(self, device, dtype):
+ points = torch.tensor(
+ [
+ [0.0, 0.0],
+ [1.0, 400.0],
+ [320.0, 240.0],
+ [319.5, 239.5],
+ [100.0, 40.0],
+ [639.0, 479.0],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ params = torch.tensor([[600.0, 600.0, 319.5, 239.5]], device=device, dtype=dtype)
+ expected = torch.tensor(
+ [
+ [319.5, 239.5],
+ [919.5, 240239.5],
+ [192319.5, 144239.5],
+ [192019.5, 143939.5],
+ [60319.5, 24239.5],
+ [383719.5, 287639.5],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ points_distorted = distort_points_affine(points, params)
+ self.assert_close(points_distorted, expected)
+ self.assert_close(points, undistort_points_affine(points_distorted, params))
+
+ def test_dx_distort_points(self, device, dtype):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5], device=device, dtype=dtype)
+ expected = torch.tensor([[600.0, 0.0], [0.0, 600.0]], device=device, dtype=dtype)
+ self.assert_close(dx_distort_points_affine(points, params), expected)
+
+ def test_exception(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5], device=device, dtype=dtype)
+ with pytest.raises(TypeError):
+ distort_points_affine(points, params)
+
+ def _test_gradcheck_distort(self, device):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5], device=device, dtype=torch.float64)
+ params = tensor_to_gradcheck_var(params)
+ self.gradcheck(distort_points_affine, (points, params))
+
+ def _test_gradcheck_undistort(self, device):
+ points = torch.tensor([601.0, 602.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5], device=device, dtype=torch.float64)
+ params = tensor_to_gradcheck_var(params)
+ self.gradcheck(undistort_points_affine, (points, params))
+
+ def test_gradcheck(self, device) -> None:
+ self._test_gradcheck_distort(device)
+ self._test_gradcheck_undistort(device)
+
+ def _test_jit_distort(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5], device=device, dtype=dtype)
+ op_script = torch.jit.script(distort_points_affine)
+ actual = op_script(points, params)
+ expected = distort_points_affine(points, params)
+ self.assert_close(actual, expected)
+
+ def _test_jit_undistort(self, device, dtype) -> None:
+ points = torch.tensor([601.0, 602.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5], device=device, dtype=dtype)
+ op_script = torch.jit.script(undistort_points_affine)
+ actual = op_script(points, params)
+ expected = undistort_points_affine(points, params)
+ self.assert_close(actual, expected)
+
+ def test_jit(self, device, dtype) -> None:
+ self._test_jit_distort(device, dtype)
+ self._test_jit_undistort(device, dtype)
+
+
+class TestDistortionKannalaBrandt(BaseTester):
+ def test_smoke(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5, 0.1, 0.2, 0.3, 0.4], device=device, dtype=dtype)
+ assert distort_points_kannala_brandt(points, params) is not None
+
+ def _test_cardinality_distort_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (2,), device=device, dtype=dtype)
+ params = torch.rand(batch_tuple + (8,), device=device, dtype=dtype)
+ assert distort_points_kannala_brandt(points, params).shape == batch_tuple + (2,)
+
+ def _test_cardinality_undistort_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (2,), device=device, dtype=dtype)
+ params = torch.rand(batch_tuple + (8,), device=device, dtype=dtype)
+ assert undistort_points_kannala_brandt(points, params).shape == batch_tuple + (2,)
+
+ @pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
+ def test_cardinality(self, device, dtype, batch_size):
+ self._test_cardinality_distort_batch(device, dtype, batch_size)
+ self._test_cardinality_undistort_batch(device, dtype, batch_size)
+
+ # NOTE: data generated with sophus-rs
+ def test_distort_points_roundtrip(self, device, dtype) -> None:
+ points = torch.tensor(
+ [
+ [0.0, 0.0],
+ [1.0, 400.0],
+ [320.0, 240.0],
+ [319.5, 239.5],
+ [100.0, 40.0],
+ [639.0, 479.0],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ params = torch.tensor(
+ [[1000.0, 1000.0, 320.0, 280.0, 0.1, 0.01, 0.001, 0.0001]],
+ device=device,
+ dtype=dtype,
+ )
+ expected = torch.tensor(
+ [
+ [320.0, 280.0],
+ [325.1949172763466, 2357.966910538644],
+ [1982.378709731326, 1526.7840322984944],
+ [1982.6832644475849, 1526.3619462760455],
+ [2235.6822069661744, 1046.2728827864696],
+ [1984.8663275417607, 1527.9983895031353],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ points_distorted = distort_points_kannala_brandt(points, params)
+ self.assert_close(points_distorted, expected)
+ self.assert_close(points, undistort_points_kannala_brandt(points_distorted, params))
+
+ def test_dx_distort_points_kannala_brandt(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5, 0.1, 0.2, 0.3, 0.4], device=device, dtype=dtype)
+ expected = torch.tensor(
+ [
+ [1191.5316162109375, 282.3212890625],
+ [282.3212890625, 1615.0135498046875],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ self.assert_close(dx_distort_points_kannala_brandt(points, params), expected)
+
+ def test_exception(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5], device=device, dtype=dtype)
+ with pytest.raises(TypeError):
+ distort_points_kannala_brandt(points, params)
+
+ def _test_gradcheck_distort(self, device):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ params = torch.tensor(
+ [600.0, 600.0, 319.5, 239.5, 0.1, 0.2, 0.3, 0.4],
+ device=device,
+ dtype=torch.float64,
+ )
+ params = tensor_to_gradcheck_var(params)
+ self.gradcheck(distort_points_kannala_brandt, (points, params))
+
+ def _test_gradcheck_undistort(self, device):
+ points = torch.tensor([919.5000, 1439.5000], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ params = torch.tensor(
+ [600.0, 600.0, 319.5, 239.5, 0.1, 0.2, 0.3, 0.4],
+ device=device,
+ dtype=torch.float64,
+ )
+ params = tensor_to_gradcheck_var(params)
+ self.gradcheck(undistort_points_kannala_brandt, (points, params))
+
+ def test_gradcheck(self, device) -> None:
+ self._test_gradcheck_distort(device)
+ self._test_gradcheck_undistort(device)
+
+ def _test_jit_distort(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5, 0.1, 0.2, 0.3, 0.4], device=device, dtype=dtype)
+ op_script = torch.jit.script(distort_points_kannala_brandt)
+ actual = op_script(points, params)
+ expected = distort_points_kannala_brandt(points, params)
+ self.assert_close(actual, expected)
+
+ def _test_jit_undistort(self, device, dtype) -> None:
+ points = torch.tensor([919.5000, 1439.5000], device=device, dtype=dtype)
+ params = torch.tensor([600.0, 600.0, 319.5, 239.5, 0.1, 0.2, 0.3, 0.4], device=device, dtype=dtype)
+ op_script = torch.jit.script(undistort_points_kannala_brandt)
+ actual = op_script(points, params)
+ expected = undistort_points_kannala_brandt(points, params)
+ self.assert_close(actual, expected)
+
+ def test_jit(self, device, dtype) -> None:
+ self._test_jit_distort(device, dtype)
+ self._test_jit_undistort(device, dtype)
diff --git a/test/geometry/camera/test_projections.py b/test/geometry/camera/test_projections.py
new file mode 100644
index 000000000..ddbc26851
--- /dev/null
+++ b/test/geometry/camera/test_projections.py
@@ -0,0 +1,235 @@
+import pytest
+import torch
+
+from kornia.geometry.camera.projection_orthographic import (
+ dx_project_points_orthographic,
+ project_points_orthographic,
+ unproject_points_orthographic,
+)
+from kornia.geometry.camera.projection_z1 import (
+ dx_project_points_z1,
+ project_points_z1,
+ unproject_points_z1,
+)
+from kornia.testing import BaseTester, tensor_to_gradcheck_var
+
+
+class TestProjectionZ1(BaseTester):
+ def test_smoke(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ assert project_points_z1(points) is not None
+
+ def _test_cardinality_unproject_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (3,), device=device, dtype=dtype)
+ assert project_points_z1(points).shape == batch_tuple + (2,)
+
+ def _test_cardinality_project_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (2,), device=device, dtype=dtype)
+ assert unproject_points_z1(points).shape == batch_tuple + (3,)
+
+ @pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
+ def test_cardinality(self, device, dtype, batch_size):
+ self._test_cardinality_project_batch(device, dtype, batch_size)
+ self._test_cardinality_unproject_batch(device, dtype, batch_size)
+
+ def test_project_points_z1(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ expected = torch.tensor([0.3333333432674408, 0.6666666865348816], device=device, dtype=dtype)
+ self.assert_close(project_points_z1(points), expected)
+
+ def test_project_points_z1_batch(self, device, dtype):
+ points = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], device=device, dtype=dtype)
+ expected = torch.tensor(
+ [
+ [0.3333333432674408, 0.6666666865348816],
+ [0.6666666865348816, 0.8333333730697632],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ self.assert_close(project_points_z1(points), expected)
+
+ def test_project_points_z1_invalid(self, device, dtype):
+ # NOTE: this is a corner case where the depth is 0.0 and the point is at infinity
+ # the projection is not defined and the function returns inf. The second point
+ # is behind the camera which is not a valid point and the user should handle it.
+ points = torch.tensor([[1.0, 2.0, 0.0], [4.0, 5.0, -1.0]], device=device, dtype=dtype)
+ expected = torch.tensor([[torch.inf, torch.inf], [-4.0, -5.0]], device=device, dtype=dtype)
+ self.assert_close(project_points_z1(points), expected)
+
+ def test_unproject_points_z1(self, device, dtype):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ expected = torch.tensor([1.0, 2.0, 1.0], device=device, dtype=dtype)
+ self.assert_close(unproject_points_z1(points), expected)
+
+ def test_unproject_points_z1_batch(self, device, dtype):
+ points = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device, dtype=dtype)
+ expected = torch.tensor([[1.0, 2.0, 1.0], [3.0, 4.0, 1.0]], device=device, dtype=dtype)
+ self.assert_close(unproject_points_z1(points), expected)
+
+ def test_project_unproject(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 2.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ self.assert_close(unproject_points_z1(project_points_z1(points), extension), points)
+
+ def test_unproject_points_z1_extension(self, device, dtype):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ expected = torch.tensor([2.0, 4.0, 2.0], device=device, dtype=dtype)
+ self.assert_close(unproject_points_z1(points, extension), expected)
+
+ def test_unproject_points_z1_batch_extension(self, device, dtype):
+ points = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device, dtype=dtype)
+ extension = torch.tensor([2.0, 3.0], device=device, dtype=dtype)
+ expected = torch.tensor([[2.0, 4.0, 2.0], [9.0, 12.0, 3.0]], device=device, dtype=dtype)
+ self.assert_close(unproject_points_z1(points, extension), expected)
+
+ def test_dx_proj_x(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ expected = torch.tensor(
+ [
+ [0.3333333432674408, 0.0, -0.1111111119389534],
+ [0.0, 0.3333333432674408, -0.2222222238779068],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ self.assert_close(dx_project_points_z1(points), expected)
+
+ def test_exception(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ with pytest.raises(TypeError):
+ unproject_points_z1(points, extension)
+
+ def _test_gradcheck_unproject(self, device):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ extension = torch.tensor([2.0], device=device, dtype=torch.float64)
+ extension = tensor_to_gradcheck_var(extension)
+ self.gradcheck(unproject_points_z1, (points, extension))
+
+ def _test_gradcheck_project(self, device):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ self.gradcheck(project_points_z1, (points,))
+
+ def test_gradcheck(self, device) -> None:
+ self._test_gradcheck_project(device)
+ self._test_gradcheck_unproject(device)
+
+ def _test_jit_unproject(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ op_script = torch.jit.script(unproject_points_z1)
+ actual = op_script(points, extension)
+ expected = unproject_points_z1(points, extension)
+ self.assert_close(actual, expected)
+
+ def _test_jit_project(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ op_script = torch.jit.script(project_points_z1)
+ actual = op_script(points)
+ expected = project_points_z1(points)
+ self.assert_close(actual, expected)
+
+ def test_jit(self, device, dtype) -> None:
+ self._test_jit_project(device, dtype)
+ self._test_jit_unproject(device, dtype)
+
+
+class TestProjectionOrthographic(BaseTester):
+ def test_smoke(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ assert project_points_orthographic(points) is not None
+
+ def _test_cardinality_unproject_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (3,), device=device, dtype=dtype)
+ assert project_points_orthographic(points).shape == batch_tuple + (2,)
+
+ def _test_cardinality_project_batch(self, device, dtype, batch_size):
+ batch_tuple = (batch_size,) if batch_size is not None else ()
+ points = torch.rand(batch_tuple + (2,), device=device, dtype=dtype)
+ extension = torch.rand(batch_tuple, device=device, dtype=dtype)
+ assert unproject_points_orthographic(points, extension).shape == batch_tuple + (3,)
+
+ @pytest.mark.parametrize("batch_size", [None, 1, 2, 3])
+ def test_cardinality(self, device, dtype, batch_size):
+ self._test_cardinality_project_batch(device, dtype, batch_size)
+ self._test_cardinality_unproject_batch(device, dtype, batch_size)
+
+ def test_project_points_orthographic(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ expected = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ self.assert_close(project_points_orthographic(points), expected)
+
+ def test_project_points_orthographic_batch(self, device, dtype):
+ points = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], device=device, dtype=dtype)
+ expected = torch.tensor([[1.0, 2.0], [4.0, 5.0]], device=device, dtype=dtype)
+ self.assert_close(project_points_orthographic(points), expected)
+
+ def test_unproject_points_orthographic_extension(self, device, dtype):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ expected = torch.tensor([1.0, 2.0, 2.0], device=device, dtype=dtype)
+ self.assert_close(unproject_points_orthographic(points, extension), expected)
+
+ def test_unproject_points_orthographic_batch_extension(self, device, dtype):
+ points = torch.tensor([[1.0, 2.0], [3.0, 4.0]], device=device, dtype=dtype)
+ extension = torch.tensor([2.0, 3.0], device=device, dtype=dtype)
+ expected = torch.tensor([[1.0, 2.0, 2.0], [3.0, 4.0, 3.0]], device=device, dtype=dtype)
+ self.assert_close(unproject_points_orthographic(points, extension), expected)
+
+ def test_project_unproject(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 2.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ self.assert_close(unproject_points_orthographic(project_points_orthographic(points), extension), points)
+
+ def test_dx_proj_x(self, device, dtype):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ expected = torch.tensor([1.0], device=device, dtype=dtype)
+ self.assert_close(dx_project_points_orthographic(points), expected)
+
+ def test_exception(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ with pytest.raises(TypeError):
+ unproject_points_orthographic(points, extension)
+
+ def _test_gradcheck_project(self, device):
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ self.gradcheck(project_points_orthographic, (points,))
+
+ def _test_gradcheck_unproject(self, device):
+ points = torch.tensor([1.0, 2.0], device=device, dtype=torch.float64)
+ points = tensor_to_gradcheck_var(points)
+ extension = torch.tensor([2.0], device=device, dtype=torch.float64)
+ extension = tensor_to_gradcheck_var(extension)
+ self.gradcheck(unproject_points_orthographic, (points, extension))
+
+ def test_gradcheck(self, device) -> None:
+ self._test_gradcheck_project(device)
+ self._test_gradcheck_unproject(device)
+
+ def _test_jit_project(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0, 3.0], device=device, dtype=dtype)
+ op_script = torch.jit.script(project_points_orthographic)
+ actual = op_script(points)
+ expected = project_points_orthographic(points)
+ self.assert_close(actual, expected)
+
+ def _test_jit_unproject(self, device, dtype) -> None:
+ points = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ extension = torch.tensor([2.0], device=device, dtype=dtype)
+ op_script = torch.jit.script(unproject_points_orthographic)
+ actual = op_script(points, extension)
+ expected = unproject_points_orthographic(points, extension)
+ self.assert_close(actual, expected)
+
+ def test_jit(self, device, dtype) -> None:
+ self._test_jit_project(device, dtype)
+ self._test_jit_unproject(device, dtype)
diff --git a/test/sensors/camera/test_projection_model.py b/test/sensors/camera/test_projection_model.py
index 80a904033..b46c16605 100644
--- a/test/sensors/camera/test_projection_model.py
+++ b/test/sensors/camera/test_projection_model.py
@@ -6,7 +6,7 @@ from kornia.sensors.camera.projection_model import Z1Projection
from kornia.testing import BaseTester
-class TestZ1Projection(BaseTester):
+class TestProjection(BaseTester):
@pytest.mark.skip(reason="Unnecessary test")
def test_smoke(self, device, dtype):
pass
@@ -34,7 +34,9 @@ class TestZ1Projection(BaseTester):
def test_project(self, device, dtype):
projection = Z1Projection()
points = torch.tensor(
- [[0.0, 0.0, 1.0], [1.0, 1.0, 1.0], [6.0, 6.0, 2.0], [9.0, 9.0, 3.0]], device=device, dtype=dtype
+ [[0.0, 0.0, 1.0], [1.0, 1.0, 1.0], [6.0, 6.0, 2.0], [9.0, 9.0, 3.0]],
+ device=device,
+ dtype=dtype,
)
expected = torch.tensor([[0.0, 0.0], [1.0, 1.0], [3.0, 3.0], [3.0, 3.0]], device=device, dtype=dtype)
self.assert_close(projection.project(Vector3(points)).data, expected)
@@ -43,9 +45,14 @@ class TestZ1Projection(BaseTester):
projection = Z1Projection()
points = torch.tensor([[0.0, 0.0], [1.0, 1.0], [3.0, 3.0], [3.0, 3.0]], device=device, dtype=dtype)
expected = torch.tensor(
- [[0.0, 0.0, 1.0], [1.0, 1.0, 1.0], [6.0, 6.0, 2.0], [9.0, 9.0, 3.0]], device=device, dtype=dtype
+ [[0.0, 0.0, 1.0], [1.0, 1.0, 1.0], [6.0, 6.0, 2.0], [9.0, 9.0, 3.0]],
+ device=device,
+ dtype=dtype,
)
self.assert_close(
- projection.unproject(Vector2(points), torch.tensor([1.0, 1.0, 2.0, 3.0], device=device, dtype=dtype)).data,
+ projection.unproject(
+ Vector2(points),
+ torch.tensor([1.0, 1.0, 2.0, 3.0], device=device, dtype=dtype),
+ ).data,
expected,
)