OpticalDistortion
Apply optical distortion (lens/camera or fisheye model) to images, masks, bboxes, keypoints. Params: distort_range, mode (camera/fisheye), interpolation.
Supports two distortion models:
-
Camera matrix model (original): Uses OpenCV's camera calibration model with k1=k2=k distortion coefficients
-
Fisheye model: Direct radial distortion: r_dist = r * (1 + gamma * r²)
distort_rangeRange of distortion coefficient, sampled per image. For camera model: recommended range (-0.05, 0.05). For fisheye model: recommended range (-0.3, 0.3). Default: (-0.05, 0.05)
modeDistortion model to use:
- 'camera': Original camera matrix model
- 'fisheye': Fisheye lens model Default: 'camera'
interpolationInterpolation method used for image transformation. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR.
mask_interpolationFlag that is used to specify the interpolation algorithm for mask. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST.
keypoint_remapping_methodMethod to use for keypoint remapping.
- "mask": Uses mask-based remapping. Faster, especially for many keypoints, but may be less accurate for large distortions. Recommended for large images or many keypoints.
- "direct": Uses inverse mapping. More accurate for large distortions but slower. Default: "mask"
map_resolution_rangeRange for sampling the displacement map resolution relative to the target size. Values below 1.0 generate lower-resolution maps and upscale them, trading precision for speed. Default: (1.0, 1.0).
pProbability of applying the transform. Default: 0.5.
>>> import albumentations as A
>>> transform = A.Compose([
... A.OpticalDistortion(distort_range=(-0.1, 0.1), p=1.0),
... ])
>>> transformed = transform(image=image, mask=mask, bboxes=bboxes, keypoints=keypoints)
>>> transformed_image = transformed['image']
>>> transformed_mask = transformed['mask']
>>> transformed_bboxes = transformed['bboxes']
>>> transformed_keypoints = transformed['keypoints']- Camera mode builds its raster map with OpenCV. Fisheye mode builds a radial map.
- The distortion coefficient (k) is randomly sampled from the distort_range range.
- Bounding boxes and keypoints are transformed along with the image to maintain consistency.
- With a full-resolution fisheye map, constant border, and a sampled coefficient in [-0.3, 0.3], keypoints and box outlines are transformed geometrically if the radius mapping is monotone in the frame. Boxes must retain a visible contour and meet BboxParams.min_visibility. KeypointParams.remove_invisible controls whether points outside the frame are kept. The keypoint remapping method selects how points are handled for other settings.