pysdic.undistort_image#

undistort_image(src, intrinsic, distortion, interpolation='linear', *, intrinsic_kwargs=None, inverse_intrinsic_kwargs=None, distortion_kwargs=None)[source]#

Undistort an image with the camera intrinsic and distortion transformations.

This function follows the same architecture as cv2.undistort, but works with any intrinsic and distortion transformations. For each pixel \(\vec{x}_i\) of the undistorted image, the position in the source image is:

\[\vec{x}_{src} = \text{Intrinsic}(\text{Distortion}(\text{Intrinsic}^{-1}(\vec{x}_i)))\]

computed with pysdic.distort_points() (P=intrinsic), and the source image is sampled at these positions. Only direct transformations are used, so no iterative inversion is needed.

The image src has the shape \((H, W, [C], [D])\). The pixel \([i, j]\) is at the image point \((x, y) = (j, i)\). The pixels sampled outside the source image are set to 0.

The available interpolations are:

Interpolation

Description

"linear"

Bilinear interpolation (default), cv2.INTER_LINEAR.

"nearest"

Nearest neighbor interpolation, cv2.INTER_NEAREST.

"cubic"

Bicubic interpolation, cv2.INTER_CUBIC.

"lanczos4"

Lanczos interpolation over 8x8 pixels, cv2.INTER_LANCZOS4.

"spline3"

Cubic spline, scipy.interpolate.RectBivariateSpline.

Note

  • The cv2 interpolations keep the type of the image, which must be uint8, uint16, int16, float32 or float64.

  • The "spline3" interpolation accepts any numeric type and returns a float64 image.

See also

Parameters:
  • src (numpy.ndarray) – The image to undistort, with shape \((H, W, [C], [D])\).

  • intrinsic (Optional[Transformation]) – The intrinsic transformation (2D to 2D). If None, no intrinsic is applied and the distortion is defined in the image coordinate system.

  • distortion (Optional[Transformation]) – The distortion transformation (2D to 2D). If None, no distortion is applied.

  • interpolation (str, optional (default: "linear")) – The interpolation method (see the table above).

  • intrinsic_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the apply method of the intrinsic transformation.

  • inverse_intrinsic_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the invert method of the intrinsic transformation.

  • distortion_kwargs (Optional[Dict[str, Any]], optional (default: None)) – Additional keyword arguments passed to the apply method of the distortion transformation.

Returns:

The undistorted image with shape \((H, W, [C], [D])\).

Return type:

numpy.ndarray

Examples

import cv2
from pysdic import undistort_image, PinholeIntrinsic, BrownDistortion

intrinsic = PinholeIntrinsic.from_matrix([[1000.0, 0.0, 320.0],
                                          [0.0, 1000.0, 240.0],
                                          [0.0, 0.0, 1.0]])
distortion = BrownDistortion([0.1, -0.05, 0.001, 0.002, 0.01])

src = cv2.imread("image.jpg")
undistorted_image = undistort_image(src, intrinsic, distortion)