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
RorPare 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.PerspectiveExtrinsicwith a rotation and no translation, as the rectification ofcv2.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=Noneto give directly the distorted points in the normalized coordinate system.Use
P=intrinsicto return the undistorted points in the image coordinate system.Use
intrinsic=NoneandP=Noneif 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
invertmethod of the distortion). Its options can be given withdistortion_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 ofjacobian_dpfor the parameters of each step.
Note
If the same transformation is given twice (for example
P=intrinsic), its parameters appear in two blocks ofjacobian_dp. The total derivative is the sum of the two blocks.See also
Class
pysdic.DistortionResultfor the result.Function
pysdic.distort_points()for the opposite direction.Function
pysdic.project_points()to project world points to image points.
- 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). Ifdp=True, the Jacobian with respect to the parameters of the transformations withTrueis not computed and its columns injacobian_dpare filled with NaN. If None, all the Jacobians are computed.intrinsic_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theinvertmethod of the intrinsic transformation.distortion_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theinvertmethod of the distortion transformation.R_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theapplymethod of \(R\).P_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theapplymethod of \(P\).
- Returns:
The normalized (or undistorted) points
xwith shape \((\dots, 2)\) and the Jacobians if requested.- Return type:
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