pydistort.undistort_image#
- pydistort.undistort_image(src: ndarray, K: ndarray | None, distortion: Distortion | None, **kwargs) ndarray [source]#
Undistort an image using the camera intrinsic and distortion coefficients.
The process to undistort an image is as follows:
The output pixels are converted to a normalized coordinate system using the intrinsic matrix K.
The normalized points are distorted by the distortion model using the coefficients \(\{\lambda_1, \lambda_2, \lambda_3, \ldots\}\).
The distorted points are projected back to the input image coordinate system using the intrinsic matrix K.
The undistorted image is obtained by mapping the pixels from the original image to the undistorted points.
The given image
src
is assumed to be in the image coordinate system and expressed in 2D coordinates with shape (H, W, [C], [D]). If the user gives an identity matrix K, it is equivalent to giving directly the normalized points.The mapping is performed using OpenCV’s cv2.remap function, which requires the source image and the mapping of pixel coordinates.
Note
For an image the X dimension corresponds to the width and the Y dimension corresponds to the height.
Pixel [0, 1] is at XY = [1, 0] in the image coordinate system.
- Parameters:
src (numpy.ndarray) – The input image to be undistorted. Shape (H, W, …) where H is the height, W is the width.
K (Optional[numpy.ndarray]) – The intrinsic camera matrix (or vector). Shape (3, 3) or (4,). If None, the identity intrinsic matrix is used.
distortion (Optional[Distortion]) – The distortion model to be applied. If None, no distortion is applied.
kwargs (dict) – Additional arguments to be passed to the distortion model “distort” method.
- Returns:
The undistorted image. Shape (H, W, …) where H is the height, W is the width.
- Return type:
numpy.ndarray
Example
import numpy from pydistort import undistort_points, Cv2Distortion # Define the intrinsic camera matrix K = numpy.array([[1000.0, 0.0, 320.0], [0.0, 1000.0, 240.0], [0.0, 0.0, 1.0]]) # Define the distortion model (optional) distortion = Cv2Distortion([0.1, 0.2, 0.3, 0.4, 0.5]) # Load the image to be undistorted src = cv2.imread('image.jpg') # Undistort the image undistorted_image = undistort_image(src, K, distortion)