pysdic.BrownDistortion#

BrownDistortion class#

class BrownDistortion(*args, **kwargs)[source]#

Bases: Transformation

Subclass of pysdic.Transformation that 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 parameters and n_parameters are provided, the length of parameters must match n_parameters.

  • If only n_parameters is provided, the parameters are not set and must be set before use (see set_zero()).

  • If only parameters is 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 of parameters.

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_parameters passed to the constructor, then initialized with zeros using set_zero().

BrownDistortion.set_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):

BrownDistortion.k1

[Get/Set] the first radial distortion coefficient k1 (component 0 of the parameters), or None if the parameters are not set.

BrownDistortion.k2

[Get/Set] the second radial distortion coefficient k2 (component 1 of the parameters), or None if the parameters are not set.

BrownDistortion.k3

[Get/Set] the third radial distortion coefficient k3 (component 4 of the parameters), or None if the parameters are not set.

BrownDistortion.k4

[Get/Set] the fourth radial distortion coefficient k4 (component 5 of the parameters), or None if the parameters are not set.

BrownDistortion.k5

[Get/Set] the fifth radial distortion coefficient k5 (component 6 of the parameters), or None if the parameters are not set.

BrownDistortion.k6

[Get/Set] the sixth radial distortion coefficient k6 (component 7 of the parameters), or None if the parameters are not set.

BrownDistortion.p1

[Get/Set] the first tangential distortion coefficient p1 (component 2 of the parameters), or None if the parameters are not set.

BrownDistortion.p2

[Get/Set] the second tangential distortion coefficient p2 (component 3 of the parameters), or None if the parameters are not set.

BrownDistortion.s1

[Get/Set] the first thin prism distortion coefficient s1 (component 8 of the parameters), or None if the parameters are not set.

BrownDistortion.s2

[Get/Set] the second thin prism distortion coefficient s2 (component 9 of the parameters), or None if the parameters are not set.

BrownDistortion.s3

[Get/Set] the third thin prism distortion coefficient s3 (component 10 of the parameters), or None if the parameters are not set.

BrownDistortion.s4

[Get/Set] the fourth thin prism distortion coefficient s4 (component 11 of the parameters), or None if the parameters are not set.

BrownDistortion.tau_x

[Get/Set] the tilt parameter along the x-axis tau_x (component 12 of the parameters), or None if the parameters are not set.

BrownDistortion.tau_y

[Get/Set] the tilt parameter along the y-axis tau_y (component 13 of the parameters), or None if the parameters are not set.

Performing Projections#

Use the apply and invert methods from pysdic.Transformation to perform distortion projections:

  • apply: maps normalized_points → distorted_points.

  • invert: maps distorted_points → normalized_points (Newton iterations, or OpenCV / pysolvegn with the opencv and pysolvegn flags).

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_points

  • dp=True: compute ∂distorted_points / ∂distortion_parameters

BrownDistortion._apply(normalized_points, *)

Compute the transformation from the normalized points to the distorted points.

BrownDistortion._invert(distorted_points, *)

Compute the inverse transformation from the distorted points to the normalized points.