pysdic.compute_rays#

compute_rays(image_points, intrinsic, distortion, extrinsic, *, intrinsic_kwargs=None, distortion_kwargs=None, extrinsic_kwargs=None)[source]#

Compute the rays emitted from the camera to the scene through the given image points.

\[\begin{split}\vec{x}_d &= \text{Intrinsic}^{-1}(\vec{x}_i) \\ \vec{x}_n &= \text{Distortion}^{-1}(\vec{x}_d) \\ \text{rays} &= \text{Extrinsic.compute\_rays}(\vec{x}_n)\end{split}\]

Where:

  • \(\vec{x}_i\) are the 2D image points in the image coordinate system.

  • \(\vec{x}_d\) are the 2D distorted points in the normalized coordinate system.

  • \(\vec{x}_n\) are the 2D normalized points in the normalized coordinate system.

The normalized points are computed with pysdic.undistort_points(), then the extrinsic transformation builds the rays in the world coordinate system with its compute_rays method (the origin and the unit direction of each ray, see pysdic.Rays).

Important

The extrinsic transformation must implement a compute_rays(normalized_points, **kwargs) method returning a pysdic.Rays object, as pysdic.PerspectiveExtrinsic, pysdic.OrthographicExtrinsic and pysdic.NoExtrinsic.

Note

The image points can be obtained from the pixel points \((u, v)\) by swapping the axes.

See also

Parameters:
  • image_points (ArrayLike) – The 2D image points in the image coordinate system. Shape \((\dots, 2)\).

  • intrinsic (Optional[Transformation]) – The intrinsic transformation (2D to 2D), inverted on the image points. If None, no intrinsic is applied (identity).

  • distortion (Optional[Transformation]) – The distortion transformation (2D to 2D), inverted on the distorted points. If None, no distortion is applied (identity).

  • extrinsic (Optional[Transformation]) – The extrinsic transformation with a compute_rays method. If None, pysdic.NoExtrinsic is used (rays parallel to the \(z\) axis).

  • intrinsic_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the invert method of the intrinsic transformation.

  • distortion_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the invert method of the distortion transformation.

  • extrinsic_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the compute_rays method of the extrinsic transformation.

Returns:

The rays in the world coordinate system, with shape \((\dots, 6)\).

Return type:

pysdic.Rays

Examples

Throw a ray through each pixel of an image:

import numpy
from pysdic import compute_rays, PinholeIntrinsic, BrownDistortion, PerspectiveExtrinsic

height, width = 480, 640
intrinsic = PinholeIntrinsic.from_matrix([[1000.0, 0.0, width / 2],
                                          [0.0, 1000.0, height / 2],
                                          [0.0, 0.0, 1.0]])
distortion = BrownDistortion([0.1, -0.05, 0.0, 0.0, 0.0])
extrinsic = PerspectiveExtrinsic.from_rt(rvec=[0.1, 0.2, 0.3], tvec=[0.0, 0.0, 5.0])

y, x = numpy.indices((height, width))
image_points = numpy.stack((x, y), axis=-1)  # (H, W, 2) with (x, y) = (column, row)

rays = compute_rays(image_points, intrinsic, distortion, extrinsic)
rays.origins     # (H, W, 3)
rays.directions  # (H, W, 3)