pysdic.ZernikeDistortion#
ZernikeDistortion class#
- class ZernikeDistortion(*args, **kwargs)[source]#
Bases:
TransformationSubclass of
pysdic.Transformationthat 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: anImportErroris 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
parameterswith 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_zernikeParameters for X or Y
Parameters of the model
n_parametersNone
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
parametersand (n_parametersorn_zernike) are provided, the length ofparametersmust matchn_parameters(see table above).If only
n_parametersorn_zernikeis 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.
- 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 fromn_zernikeor from the length ofparameters.n_zernike (Optional[Integral], optional (default:
None)) – Maximum order \(N_{zer}\) of the Zernike polynomials. If not specified, it is inferred fromn_parametersor from the length ofparameters.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 toradius_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 byapply(), possibly shared with other distortions (seeZernikePolynomialsCache). By default, no cache: the polynomials are computed at each call. It can be set or removed (None) at any time with the propertypolynomials_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 parametersn_parameters) passed to the constructor, then initialized with zeros usingset_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).
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:
[Get] the center \((x_0, y_0)\) of the ellipse of definition. |
|
[Get] the x coordinate \(x_0\) of the center of the ellipse. |
|
[Get] the y coordinate \(y_0\) of the center of the ellipse. |
|
[Get] the maximum order \(N_{zer}\) of the Zernike polynomials, such that \(N_{params} = (N_{zer}+1)(N_{zer}+2)\). |
|
|
|
|
|
[Get/Set] the Zernike coefficients \(C^{x}_{n,m}\) for the x coordinate in the OSA/ANSI order, with shape |
|
[Get/Set] the Zernike coefficients \(C^{y}_{n,m}\) for the y coordinate in the OSA/ANSI order, with shape |
|
[Get/Set] The cache of the Zernike polynomials used by |
|
[Get] the radius \(R = R_x = R_y\) of the disk of definition. |
|
[Get] the radius \(R_x\) of the ellipse of definition along x. |
|
[Get] the radius \(R_y\) of the ellipse of definition along y. |
The individual Zernike coefficients can be accessed with the following methods:
|
Get the index in the |
|
Get the Zernike coefficient \(C^{x}_{n,m}\) for the x coordinate. |
|
Get the Zernike coefficient \(C^{y}_{n,m}\) for the y coordinate. |
|
Set the Zernike coefficient \(C^{x}_{n,m}\) for the x coordinate. |
|
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: mapsnormalized_points→distorted_points(NaN outside the ellipse of definition).invert: mapsdistorted_points→normalized_points(Newton iterations, orpysolvegnwith thepysolvegnflag).
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. |
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.