pysdic.distort_image#
- distort_image(src, intrinsic, distortion, method='undistort', interpolation='linear', *, intrinsic_kwargs=None, inverse_intrinsic_kwargs=None, distortion_kwargs=None, inverse_distortion_kwargs=None)[source]#
Distort an image with the camera intrinsic and distortion transformations.
This is the opposite of
pysdic.undistort_image(): the source image is a perfect (undistorted) image and the output is the image seen through the distortion. Two methods are available.Method
"undistort"(default): for each pixel \(\vec{x}_i\) of the distorted image, the position in the source image is:\[\vec{x}_{src} = \text{Intrinsic}(\text{Distortion}^{-1}(\text{Intrinsic}^{-1}(\vec{x}_i)))\]computed with
pysdic.undistort_points()(P=intrinsic), and the source image is sampled on its regular grid of pixels (fast). The inverse of the distortion is usually computed with an iterative method, whose options can be given withinverse_distortion_kwargs. The interpolations are the same as forpysdic.undistort_image()("linear","nearest","cubic","lanczos4","spline3").Method
"distort": each pixel of the source image is moved to its distorted position:\[\vec{x}_{dst} = \text{Intrinsic}(\text{Distortion}(\text{Intrinsic}^{-1}(\vec{x}_{src})))\]computed with
pysdic.distort_points()(P=intrinsic), and the distorted image is interpolated on its grid of pixels from this irregular cloud of points (slow, but no inverse of the distortion is needed). The available interpolations are:Interpolation
Description
"linear"Linear interpolation (default),
scipy.interpolate.LinearNDInterpolator."nearest"Nearest neighbor,
scipy.interpolate.NearestNDInterpolator."clough"Piecewise cubic,
scipy.interpolate.CloughTocher2DInterpolator.The image
srchas the shape \((H, W, [C], [D])\). The pixel \([i, j]\) is at the image point \((x, y) = (j, i)\). The pixels outside the source image (or outside the cloud of points) are set to 0.Note
The
cv2interpolations keep the type of the image, which must beuint8,uint16,int16,float32orfloat64.The
"spline3"interpolation and the method"distort"accept any numeric type and return afloat64image.
See also
Function
pysdic.undistort_image()for the opposite operation.
- Parameters:
src (numpy.ndarray) – The image to distort, 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.
method (str, optional (default:
"undistort")) – The method,"undistort"or"distort".interpolation (str, optional (default:
"linear")) – The interpolation method, depending on the method (see above).intrinsic_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theapplymethod of the intrinsic transformation.inverse_intrinsic_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theinvertmethod of the intrinsic transformation.distortion_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theapplymethod of the distortion transformation (method"distort").inverse_distortion_kwargs (Optional[Dict[str, Any]], optional (default:
None)) – Additional keyword arguments passed to theinvertmethod of the distortion transformation (method"undistort").
- Returns:
The distorted image with shape \((H, W, [C], [D])\).
- Return type:
Examples
distorted_image = distort_image(src, intrinsic, distortion) distorted_image = distort_image(src.astype(float), intrinsic, distortion, method="distort")