pysdic.ZernikeDistortion#

ZernikeDistortion class#

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

Bases: Transformation

Subclass of pysdic.Transformation that represents a distortion transformation based on Zernike polynomials.

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. The corresponding distorted point \(\vec{x}_d = (x_d, y_d)\) is obtained by adding a displacement field expanded on the Zernike polynomials \(Z_{n,m}\):

\[x_{d} = x_{n} + \sum_{n=0}^{N_{zer}} \sum_{m} C^{x}_{n,m} Z_{n,m}(\rho, \theta)\]
\[y_{d} = y_{n} + \sum_{n=0}^{N_{zer}} \sum_{m} C^{y}_{n,m} Z_{n,m}(\rho, \theta)\]

where \(m \in [-n, n]\) with \(n - m\) even, \(C^{x}_{n,m}\) and \(C^{y}_{n,m}\) are the Zernike coefficients for the x and y coordinates, and \(\rho\) and \(\theta\) are the polar coordinates of the normalized points in the ellipse of definition with radii \((R_x, R_y)\) and center \((x_0, y_0)\):

\[\rho = \sqrt{\left(\frac{x_{n} - x_{0}}{R_x}\right)^2 + \left(\frac{y_{n} - y_{0}}{R_y}\right)^2}, \qquad \theta = \arctan2\left(\frac{y_{n} - y_{0}}{R_y}, \frac{x_{n} - x_{0}}{R_x}\right)\]

Note

The Zernike polynomials are only defined inside the ellipse (\(\rho \leq 1\)). The points outside the ellipse are distorted to NaN, so the ellipse must contain the whole field of view of the camera.

See also

Package pyzernike (Artezaru/pyzernike) for the implementation of the Zernike polynomials and their derivatives.

Note

This class requires pyzernike: an ImportError is raised at the instantiation if it is missing (ZernikeDistortion.is_available() returns False).

The transformation is parameterized by the Zernike coefficients \(C^{x}_{n,m}\) and \(C^{y}_{n,m}\) for \(n \leq N_{zer}\), and depends on 4 constants: the radii \((R_x, R_y)\) and the center \((x_0, y_0)\) of the ellipse.

The coefficients are stored in the parameters with the OSA/ANSI standard indices \(j = \frac{n(n+2) + m}{2}\), alternating the x and y coordinates:

parameters[2j]     = C^x_{n,m}
parameters[2j + 1] = C^y_{n,m}

i.e. \((C^x_{0,0}, C^y_{0,0}, C^x_{1,-1}, C^y_{1,-1}, C^x_{1,1}, C^y_{1,1}, C^x_{2,-2}, \dots)\). The number of parameters is:

\[N_{params} = (N_{zer}+1)(N_{zer}+2)\]

Order n_zernike

Parameters for X or Y

Parameters of the model n_parameters

None

0

0

0

1

2

1

3

6

2

6

12

3

10

20

4

15

30

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 inside the ellipse).

Important

During instantiation:

  • If both parameters and (n_parameters or n_zernike) are provided, the length of parameters must match n_parameters (see table above).

  • If only n_parameters or n_zernike 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.

Parameters:
  • parameters (Optional[ArrayLike], optional (default: None)) – Distortion parameters of shape (n_parameters,) ordered as described above. If None, the parameters can be set later.

  • n_parameters (Optional[Integral], optional (default: None)) – Number of parameters of the distortion model, in [0, 2, 6, 12, 20, 30, ...]. If not specified, it is inferred from n_zernike or from the length of parameters.

  • n_zernike (Optional[Integral], optional (default: None)) – Maximum order \(N_{zer}\) of the Zernike polynomials. If not specified, it is inferred from n_parameters or from the length of parameters.

  • radius_x (Real, optional (default: 1.0)) – The radius \(R_x\) of the ellipse of definition along x (strictly positive).

  • radius_y (Optional[Real], optional (default: None)) – The radius \(R_y\) of the ellipse of definition along y (strictly positive). If None, it is equal to radius_x (circle).

  • center (ArrayLike, optional (default: (0.0, 0.0))) – The center \((x_0, y_0)\) of the ellipse of definition.

  • polynomials_cache (Optional[ZernikePolynomialsCache], optional (default: None)) – A cache of the Zernike polynomials used by apply(), possibly shared with other distortions (see ZernikePolynomialsCache). By default, no cache: the polynomials are computed at each call. It can be set or removed (None) at any time with the property polynomials_cache. It is not serialized and not copied, and it never changes the results.

Examples

A Zernike distortion up to the order 2 defined on the disk of radius 1.5:

distortion = ZernikeDistortion(n_zernike=2, radius_x=1.5)
distortion.set_zero()
distortion.set_Cx(2, 0, 0.01)   # Defocus-like term along x

distorted = distortion.apply([[0.1, 0.2]]).x
distortion.invert(distorted).x    # array([[0.1, 0.2]])

Instantiation#

The ZernikeDistortion class can be instantiated in two ways:

  • From a parameter vector of shape (N_params,) with \(N_{params} = (N_{zer}+1)(N_{zer}+2)\) passed to the constructor.

  • From a Zernike order n_zernike (or a number of parameters n_parameters) passed to the constructor, then initialized with zeros using set_zero().

The ellipse of definition is set at the instantiation with the constants radius_x, radius_y and center. A cache of the Zernike polynomials can be given with polynomials_cache (see ZernikePolynomialsCache).

ZernikeDistortion.set_zero()

Set all the Zernike coefficients to zero (no distortion).

Implemented methods and attributes#

The following sections provide an overview of the attributes and methods defined in the pysdic.ZernikeDistortion class.

Attributes#

The distortion parameters can be accessed via the base class pysdic.Transformation:

  • parameters: returns a vector of distortion parameters [Cx(0, 0), Cy(0, 0), Cx(1, -1), Cy(1, -1), ...] (OSA/ANSI order, alternating x and y).

The pysdic.ZernikeDistortion defines the following attributes:

ZernikeDistortion.center

[Get] the center \((x_0, y_0)\) of the ellipse of definition.

ZernikeDistortion.center_x

[Get] the x coordinate \(x_0\) of the center of the ellipse.

ZernikeDistortion.center_y

[Get] the y coordinate \(y_0\) of the center of the ellipse.

ZernikeDistortion.n_zernike

[Get] the maximum order \(N_{zer}\) of the Zernike polynomials, such that \(N_{params} = (N_{zer}+1)(N_{zer}+2)\).

ZernikeDistortion.parameter_x_names

["Cx(0, 0)", "Cx(1, -1)", "Cx(1, 1)", ...].

ZernikeDistortion.parameter_y_names

["Cy(0, 0)", "Cy(1, -1)", "Cy(1, 1)", ...].

ZernikeDistortion.parameters_x

[Get/Set] the Zernike coefficients \(C^{x}_{n,m}\) for the x coordinate in the OSA/ANSI order, with shape (n_parameters // 2,).

ZernikeDistortion.parameters_y

[Get/Set] the Zernike coefficients \(C^{y}_{n,m}\) for the y coordinate in the OSA/ANSI order, with shape (n_parameters // 2,).

ZernikeDistortion.polynomials_cache

[Get/Set] The cache of the Zernike polynomials used by apply(), or None (default) for no cache (see ZernikePolynomialsCache).

ZernikeDistortion.radius

[Get] the radius \(R = R_x = R_y\) of the disk of definition.

ZernikeDistortion.radius_x

[Get] the radius \(R_x\) of the ellipse of definition along x.

ZernikeDistortion.radius_y

[Get] the radius \(R_y\) of the ellipse of definition along y.

The individual Zernike coefficients can be accessed with the following methods:

ZernikeDistortion.get_index(n, m, coord)

Get the index in the parameters of the Zernike coefficient \(C^{x}_{n,m}\) or \(C^{y}_{n,m}\).

ZernikeDistortion.get_Cx(n, m)

Get the Zernike coefficient \(C^{x}_{n,m}\) for the x coordinate.

ZernikeDistortion.get_Cy(n, m)

Get the Zernike coefficient \(C^{y}_{n,m}\) for the y coordinate.

ZernikeDistortion.set_Cx(n, m, value)

Set the Zernike coefficient \(C^{x}_{n,m}\) for the x coordinate.

ZernikeDistortion.set_Cy(n, m, value)

Set the Zernike coefficient \(C^{y}_{n,m}\) for the y coordinate.

Performing Projections#

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

  • apply: maps normalized_points → distorted_points (NaN outside the ellipse of definition).

  • invert: maps distorted_points → normalized_points (Newton iterations, or pysolvegn with the pysolvegn flag).

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

ZernikeDistortion._apply(normalized_points, *)

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

ZernikeDistortion._invert(distorted_points, *)

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

Cache of the Zernike polynomials#

The Zernike polynomials only depend on the points and on the ellipse of definition, not on the coefficients: when the distortion is applied several times to the same points (e.g. the calibration of the distortion alone), they can be computed once and reused with a pysdic.ZernikePolynomialsCache, attached with the property polynomials_cache (no cache by default).

See also

ZernikePolynomialsCache for the usage, the rules and the memory of the cache.