Cv2Distortion Class#

Overview#

class Cv2Distortion(parameters=None, constants=None, n_params=None)[source]#

Subclass of the pycvcam.Distortion class that represents the OpenCV distortion model.

Note

This class represents the distortion transformation, which is the middle step of the process from the world_points to the image_points.

The Cv2Distortion model 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_params property. The internal parameters array will be set to None after changing the number of parameters, and must be re-initialized with the new number of parameters before use.

During instantiation:

  • If both parameters and n_params are provided, the length of parameters must match n_params.

  • If only n_params is provided, the parameters array will be None and must be set before use.

  • If only parameters is provided, the number of parameters is inferred from the length of the array.

  • If neither is provided, n_params defaults 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 None or 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:

Cv2Distortion.k1

[Get/Set] the first radial distortion coefficient.

Cv2Distortion.k2

[Get/Set] the second radial distortion coefficient.

Cv2Distortion.k3

[Get/Set] the third radial distortion coefficient.

Cv2Distortion.k4

[Get/Set] the fourth radial distortion coefficient.

Cv2Distortion.k5

[Get/Set] the fifth radial distortion coefficient.

Cv2Distortion.k6

[Get/Set] the sixth radial distortion coefficient.

Cv2Distortion.p1

[Get/Set] the first tangential distortion coefficient.

Cv2Distortion.p2

[Get/Set] the second tangential distortion coefficient.

Cv2Distortion.s1

[Get/Set] the first thin prism distortion coefficient.

Cv2Distortion.s2

[Get/Set] the second thin prism distortion coefficient.

Cv2Distortion.s3

[Get/Set] the third thin prism distortion coefficient.

Cv2Distortion.s4

[Get/Set] the fourth thin prism distortion coefficient.

Cv2Distortion.tau_x

[Get/Set] the tilt parameter along the x-axis.

Cv2Distortion.tau_y

[Get/Set] the tilt parameter along the y-axis.

Cv2Distortion.set_zero()

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: maps normalized_points → distorted_points.

  • inverse_transform: maps distorted_points → normalized_points.

Jacobian computation:

  • dx=True: compute ∂distorted_points / ∂normalized_points

  • dp=True: compute ∂distorted_points / ∂distortion_parameters

Cv2Distortion._transform(normalized_points, *)

Compute the transformation from the normalized_points to the distorted_points.

Cv2Distortion._transform_opencv(...[, dx, dp])

Compute the transformation from the normalized_points to the distorted_points using OpenCV's projectPoints function.

Cv2Distortion._inverse_transform(...[, dx, ...])

Compute the inverse transformation from the distorted_points to the normalized_points.

Cv2Distortion._inverse_transform_opencv(...)

Compute the inverse transformation from the distorted_points to the normalized_points using OpenCV's undistortPoints function.

Cv2Distortion._compute_tilt_matrix([dp, inv])

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)