AlbumentationsExplore
DocumentationExploreAdoptionPricingBlog
GitHub

ElasticTransform

Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float32

Apply bounded XY deformations from a compact control grid to images and annotations. Use it for shape variation in segmentation and medical imaging.

displacement_range is measured relative to the shorter span between the first and last pixel centers. The sampled cubic B-spline coefficients use pixel units after scaling. One map is shared by every raster and annotation target in an invocation; volumes receive the same XY deformation on every depth slice.

Arguments
displacement_range
tuple[float, float]
[0.02, 0.05]

Range for the sampled relative displacement magnitude.

control_grid_shape
tuple[int, int]
[7, 7]

Number of cubic B-spline coefficient rows and columns, each at least 4.

interpolation
0 | 1 | 2 | 3 | 4
1

Interpolation used for images.

mask_interpolation
0 | 1 | 2 | 3 | 4
0

Interpolation used for masks.

border_mode
0 | 1 | 2 | 3 | 4
0

OpenCV border mode for raster targets.

fill
tuple[float, ...] | float
0

Fill value for images.

fill_mask
tuple[float, ...] | float
0

Fill value for masks.

p
float
0.5

Probability of applying the transform.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.zeros((100, 100, 3), dtype=np.uint8)
>>> mask = np.zeros((100, 100), dtype=np.uint8)
>>> bboxes = np.array([[10, 10, 50, 50]], dtype=np.float32)
>>> bbox_labels = [1]
>>> keypoints = np.array([[20, 30]], dtype=np.float32)
>>> keypoint_labels = [0]
>>> transform = A.Compose(
...     [A.ElasticTransform(displacement_range=(0.02, 0.05), control_grid_shape=(7, 7), p=1.0)],
...     bbox_params=A.BboxParams(coord_format="pascal_voc", label_fields=["bbox_labels"]),
...     keypoint_params=A.KeypointParams(
...         coord_format="xy", label_fields=["keypoint_labels"], label_mapping={}
...     ),
... )
>>> transformed = transform(
...     image=image,
...     mask=mask,
...     bboxes=bboxes,
...     bbox_labels=bbox_labels,
...     keypoints=keypoints,
...     keypoint_labels=keypoint_labels,
... )
>>> transformed_image = transformed["image"]
>>> transformed_mask = transformed["mask"]
>>> transformed_bboxes = transformed["bboxes"]
>>> transformed_bbox_labels = transformed["bbox_labels"]
>>> transformed_keypoints = transformed["keypoints"]
>>> transformed_keypoint_labels = transformed["keypoint_labels"]
Notes

The constructor enforces 2 * high * sqrt((rows - 3)^2 + (columns - 3)^2) < 0.75. ReplayCompose stores the compact sampled coefficient lattice and replays it for the same spatial shape. Applied configuration fixes the realized magnitude but samples a new lattice.