pysdic.undistort_points#

undistort_points(image_points, intrinsic, distortion, R=None, P=None, *, dx=False, dp=False, bypass_dp=None, intrinsic_kwargs=None, distortion_kwargs=None, R_kwargs=None, P_kwargs=None)[source]#

Undistort 2D image points \(\vec{x}_i\) with the inverses of the camera intrinsic and distortion transformations, to obtain the normalized points \(\vec{x}_n\), or the undistorted points \(\vec{x}_u\) if R or P are given.

\[\begin{split}\vec{x}_d &= \text{Intrinsic}^{-1}(\vec{x}_i) \\ \vec{x}_n &= \text{Distortion}^{-1}(\vec{x}_d) \\ \vec{x}_u &= P(R(\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.

  • \(\vec{x}_u\) are the 2D undistorted points in the space required by the user.

If \(R\) takes 3D points (for example a pysdic.PerspectiveExtrinsic with a rotation and no translation, as the rectification of cv2.undistortPoints), the normalized points are first lifted to \((x_n, y_n, 1)\).

Note

The behavior of the function can be adapted by giving the appropriate transformations:

  • Use intrinsic=None to give directly the distorted points in the normalized coordinate system.

  • Use P=intrinsic to return the undistorted points in the image coordinate system.

  • Use intrinsic=None and P=None if the distortion model is defined in the image coordinate system.

Warning

The inverse of most distortion models is computed with an iterative method (see the invert method of the distortion). Its options can be given with distortion_kwargs.

The Jacobians are obtained with the chain rule (as for pysdic.project_points()) and can be accessed with the attributes of the result:

  • jacobian_dx: The Jacobian of the output points with respect to the image points. Shape \((\dots, 2, 2)\).

  • jacobian_dp: The Jacobian of the output points with respect to all the parameters. Shape \((\dots, 2, N_i + N_d + N_R + N_P)\).

  • jacobian_dintrinsic, jacobian_ddistortion, jacobian_drectification, jacobian_dprojection: Views of jacobian_dp for the parameters of each step.

Note

If the same transformation is given twice (for example P=intrinsic), its parameters appear in two blocks of jacobian_dp. The total derivative is the sum of the two blocks.

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).

  • R (Optional[Transformation], optional (default: None)) – The rectification transformation applied to the normalized points, from 2D or 3D points to 2D points. If None, no rectification is applied.

  • P (Optional[Transformation], optional (default: None)) – The projection transformation (2D to 2D) applied after the rectification. If None, no projection is applied.

  • dx (bool, optional (default: False)) – If True, compute the Jacobian of the output points with respect to the image points.

  • dp (bool, optional (default: False)) – If True, compute the Jacobian of the output points with respect to the parameters of the intrinsic, distortion, \(R\) and \(P\) transformations.

  • bypass_dp (Optional[Sequence[bool]], optional (default: None)) – Four booleans for the intrinsic, distortion, \(R\) and \(P\) transformations (in this order). If dp=True, the Jacobian with respect to the parameters of the transformations with True is not computed and its columns in jacobian_dp are filled with NaN. If None, all the Jacobians are computed.

  • 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.

  • R_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the apply method of \(R\).

  • P_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the apply method of \(P\).

Returns:

The normalized (or undistorted) points x with shape \((\dots, 2)\) and the Jacobians if requested.

Return type:

pysdic.DistortionResult

Examples

Undistort image points with a pinhole intrinsic and a Brown distortion:

import numpy
from pysdic import undistort_points, PinholeIntrinsic, BrownDistortion

image_points = numpy.array([[320.0, 240.0], [420.0, 440.0], [520.0, 540.0]])

intrinsic = PinholeIntrinsic.from_matrix([[1000.0, 0.0, 320.0],
                                          [0.0, 1000.0, 240.0],
                                          [0.0, 0.0, 1.0]])
distortion = BrownDistortion([0.1, -0.05, 0.001, 0.002, 0.01])

normalized_points = undistort_points(image_points, intrinsic, distortion).x

To return the undistorted points in the image coordinate system, give P=intrinsic:

undistorted_points = undistort_points(image_points, intrinsic, distortion, P=intrinsic).x