pysdic.BrownDistortion#
BrownDistortion class#
- class BrownDistortion(*args, **kwargs)[source]#
Bases:
TransformationSubclass of
pysdic.Transformationthat represents the Brown-Conrady distortion model, as implemented by OpenCV, with radial, tangential, thin prism, and tilt distortions.This class represents a distortion transformation, which is typically the middle step in the camera projection pipeline:
world_points → normalized_points → distorted_points → image_points
Let \(\vec{x}_n = (x_n, y_n)\) be a normalized point and \(r^2 = x_n^2 + y_n^2\). The distortion without tilt \(\vec{x}_0 = (x_0, y_0)\) is given by the OpenCV model:
\[\begin{split}\begin{bmatrix} x_0 \\ y_0 \end{bmatrix} = \begin{bmatrix} x_n K(r^2) + 2p_1 x_n y_n + p_2 (r^2 + 2x_n^2) + s_1 r^2 + s_2 r^4 \\ y_n K(r^2) + p_1 (r^2 + 2y_n^2) + 2p_2 x_n y_n + s_3 r^2 + s_4 r^4 \end{bmatrix} \qquad \text{with} \qquad K(r^2) = \frac{1+k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6}\end{split}\]where
\(k_i\) are the radial distortion coefficients,
\(p_i\) are the tangential distortion coefficients,
\(s_i\) are the thin prism distortion coefficients.
The distorted point \(\vec{x}_d = (x_d, y_d)\) is then obtained by the tilt projection of parameters \((\tau_x, \tau_y)\):
\[\begin{split}\begin{bmatrix} h_x \\ h_y \\ h_z \end{bmatrix} = M(\tau) \begin{bmatrix} x_0 \\ y_0 \\ 1 \end{bmatrix}, \qquad x_d = \frac{h_x}{h_z}, \qquad y_d = \frac{h_y}{h_z}\end{split}\]where \(M(\tau) = R_Z[R(\tau)] \, R(\tau)\) is the tilt matrix of OpenCV (see
_compute_tilt_matrix()). Without tilt parameters, \(M = I_3\) and \(\vec{x}_d = \vec{x}_0\).OpenCV supports several distortion configurations depending on the number of parameters \(N\):
N = 0 : no distortion (identity)
N = 4 : \((k_1, k_2, p_1, p_2)\) — radial and tangential distortion
N = 5 : \((k_1, k_2, p_1, p_2, k_3)\) — additional radial term
N = 8 : \((k_1, k_2, p_1, p_2, k_3, k_4, k_5, k_6)\) — rational radial model
N = 12 : \((k_1, k_2, p_1, p_2, k_3, k_4, k_5, k_6, s_1, s_2, s_3, s_4)\) — adds thin prism distortion
N = 14 : previous parameters plus \((\tau_x, \tau_y)\) — tilt distortion
The missing coefficients of a configuration are equal to zero.
The inverse transformation (undistortion) has no closed form and is computed with Newton iterations (see
_invert()). It is available with its Jacobians, and it is both a left and a right inverse of the distortion in the domain where the distortion is bijective (i.e. for reasonable distortions and points in the field of view).See also
OpenCV calibration documentation: https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html
Important
During instantiation:
If both
parametersandn_parametersare provided, the length ofparametersmust matchn_parameters.If only
n_parametersis provided, the parameters are not set and must be set before use (seeset_zero()).If only
parametersis provided, the number of parameters is inferred from the length of the array.If neither is provided, an error is raised.
- Parameters:
parameters (Optional[ArrayLike], optional (default:
None)) – Distortion parameters of shape(N,)ordered according to the selected OpenCV model.n_parameters (Optional[Integral], optional (default:
None)) – Number of parameters \(N\) of the distortion model, in[0, 4, 5, 8, 12, 14]. If not specified, it is inferred from the length ofparameters.
Examples
A radial and tangential distortion (5 parameters, as returned by
cv2.calibrateCamera):distortion = BrownDistortion([-0.1, 0.01, 0.001, -0.002, 0.0]) distorted = distortion.apply([[0.1, 0.2]]).x distortion.invert(distorted).x # array([[0.1, 0.2]]) distortion.k1 # -0.1
Instantiation#
The BrownDistortion class can be instantiated in two ways:
From a parameter vector of shape
(N,)with \(N \in \{0, 4, 5, 8, 12, 14\}\) passed to the constructor.From a number of parameters
n_parameterspassed to the constructor, then initialized with zeros usingset_zero().
Set all the parameters of the distortion model to zero (no distortion). |
Implemented methods and attributes#
The following sections provide an overview of the attributes and methods defined
in the pysdic.BrownDistortion class.
Attributes#
The distortion parameters can be accessed via the base class
pysdic.Transformation:
parameters: returns a vector of distortion parameters, the first \(N\) of[k1, k2, p1, p2, k3, k4, k5, k6, s1, s2, s3, s4, tau_x, tau_y].
The pysdic.BrownDistortion defines the following attributes
(each coefficient is only available if the model has enough parameters):
[Get/Set] the first radial distortion coefficient |
|
[Get/Set] the second radial distortion coefficient |
|
[Get/Set] the third radial distortion coefficient |
|
[Get/Set] the fourth radial distortion coefficient |
|
[Get/Set] the fifth radial distortion coefficient |
|
[Get/Set] the sixth radial distortion coefficient |
|
[Get/Set] the first tangential distortion coefficient |
|
[Get/Set] the second tangential distortion coefficient |
|
[Get/Set] the first thin prism distortion coefficient |
|
[Get/Set] the second thin prism distortion coefficient |
|
[Get/Set] the third thin prism distortion coefficient |
|
[Get/Set] the fourth thin prism distortion coefficient |
|
[Get/Set] the tilt parameter along the x-axis |
|
[Get/Set] the tilt parameter along the y-axis |
Performing Projections#
Use the apply and invert methods from
pysdic.Transformation to perform distortion projections:
apply: mapsnormalized_points→distorted_points.invert: mapsdistorted_points→normalized_points(Newton iterations, or OpenCV /pysolvegnwith theopencvandpysolvegnflags).
Jacobians with respect to the input points and distortion parameters
can be computed by enabling the dx and dp flags:
dx=True: compute ∂distorted_points / ∂normalized_pointsdp=True: compute ∂distorted_points / ∂distortion_parameters
|
Compute the transformation from the normalized points to the distorted points. |
|
Compute the inverse transformation from the distorted points to the normalized points. |