Cv2Distortion Class#
Overview#
- class Cv2Distortion(parameters=None, constants=None, n_params=None)[source]#
Subclass of the
pycvcam.Distortionclass that represents the OpenCV distortion model.Note
This class represents the distortion transformation, which is the middle step of the process from the
world_pointsto theimage_points.The
Cv2Distortionmodel in the OpenCV model with radial, tangential and prism distortion.Let \(\vec{x}_n = (x_n, y_n)\) be a point in normalized camera coordinates. The distorted point \(\vec{x}_d = (x_d, y_d)\) is obtained by applying the OpenCV distortion model.
Radial, tangential, and thin prism distortions are defined as:
\[r^2 = x_n^2 + y_n^2\]\[\begin{split}\begin{bmatrix} x_d \\ y_d \end{bmatrix} = \begin{bmatrix} x_n \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} + 2p_1 x_n y_n + p_2 (r^2 + 2x_n^2) + s_1 r^2 + s_2 r^4 \\ y_n \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} + p_1 (r^2 + 2y_n^2) + 2p_2 x_n y_n + s_3 r^2 + s_4 r^4 \end{bmatrix}\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.
When tilt parameters \((\tau_x, \tau_y)\) are present, an additional perspective transformation is applied using the rotation matrix \(R(\tau)\):
\[\begin{split}R(\tau) = \begin{bmatrix} \cos(\tau_y) & \sin(\tau_x)\sin(\tau_y) & -\cos(\tau_x)\sin(\tau_y) \\ 0 & \cos(\tau_x) & \sin(\tau_x) \\ \sin(\tau_y) & -\sin(\tau_x)\cos(\tau_y) & \cos(\tau_x)\cos(\tau_y) \end{bmatrix}\end{split}\]The distorted homogeneous coordinates are then obtained by applying the tilt projection described in the OpenCV documentation.
See also
OpenCV calibration documentation: https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html
OpenCV supports several distortion configurations depending on the number of parameters:
N = 0 : no distortion
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
Important
To update the number of parameters after initialization, set the
n_paramsproperty. The internal parameters array will be set toNoneafter changing the number of parameters, and must be re-initialized with the new number of parameters before use.During instantiation:
If both
parametersandn_paramsare provided, the length ofparametersmust matchn_params.If only
n_paramsis provided, the parameters array will be None and must be set before use.If only
parametersis provided, the number of parameters is inferred from the length of the array.If neither is provided,
n_paramsdefaults to 0 and the parameters array is None.
- Parameters:
parameters (Optional[ArrayLike], optional) –
Distortion parameters of shape
(n_params,)ordered according to the selected OpenCV model.If
None(default), the parameters can be set later.constants (Optional[ArrayLike], optional) – Must be
Noneor empty, as this model does not use constants.n_params (Optional[int], optional) – Number of parameters of the distortion model. If not specified, it is inferred from the length of
parameters.
Accessing Parameters#
The distortion parameters and constants can be accessed via the base class
pycvcam.Transform:
parameters: returns a vector of distortion coefficients [k1, k2, k3, …, tau_x, tau_y].constants: returns any constants (empty for Cv2Distortion).
Convenience properties are provided for direct access:
[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. |
|
Set to zero the parameters of the distortion model. |
Performing Distortion#
Use the transform and inverse_transform methods from
pycvcam.Transform to perform distortion and undistortion:
transform: mapsnormalized_points→distorted_points.inverse_transform: mapsdistorted_points→normalized_points.
Jacobian computation:
dx=True: compute ∂distorted_points / ∂normalized_pointsdp=True: compute ∂distorted_points / ∂distortion_parameters
|
Compute the transformation from the |
|
Compute the transformation from the |
|
Compute the inverse transformation from the |
Compute the inverse transformation from the |
|
|
Compute the tilt matrix for the perspective transformation for N = 14 (only). |
Examples#
Create a distortion object with a specific number of parameters:
import numpy
from pycvcam import Cv2Distortion
parameters = numpy.array([0.1, 0.01, 0.02, 0.03, 0.001])
distortion = Cv2Distortion(parameters=parameters)
Transform normalized points to distorted points:
normalized_points = numpy.array([[0.1, 0.2],
[0.3, 0.4],
[0.5, 0.6]])
result = distortion.transform(normalized_points)
distorted_points = result.distorted_points
print(distorted_points)
Compute Jacobians:
result = distortion.transform(normalized_points, dx=True, dp=True)
distorted_points_dx = result.jacobian_dx
distorted_points_dp = result.jacobian_dp
print(distorted_points_dx)
print(distorted_points_dp)
Inverse transformation:
inverse_result = distortion.inverse_transform(distorted_points, dx=True, dp=True)
normalized_points = inverse_result.normalized_points
print(normalized_points)