GridElasticDeform
Apply elastic deformations to images, masks, bounding boxes, and keypoints using a grid-based approach.
This transformation overlays a grid on the input and applies random displacements to the grid points, resulting in local elastic distortions. The granularity and intensity of the distortions can be controlled using the dimensions of the overlaying distortion grid and the magnitude parameter.
num_grid_xyNumber of grid cells along the width and height. Specified as (grid_width, grid_height). Each value must be greater than 1.
magnitudeMaximum pixel-wise displacement for distortion. Must be greater than 0.
interpolationInterpolation method to be used for the image transformation. Default: cv2.INTER_LINEAR
mask_interpolationInterpolation method to be used for mask transformation. Default: cv2.INTER_NEAREST
pProbability of applying the transform. Default: 1.0.
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Prepare sample data
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> bboxes = np.array([[10, 10, 50, 50], [40, 40, 80, 80]], dtype=np.float32)
>>> bbox_labels = [1, 2]
>>> keypoints = np.array([[20, 30], [60, 70]], dtype=np.float32)
>>> keypoint_labels = [0, 1]
>>>
>>> # Define transform with parameters as tuples when possible
>>> transform = A.Compose([
... A.GridElasticDeform(
... num_grid_xy=(4, 4),
... magnitude=10,
... interpolation=cv2.INTER_LINEAR,
... mask_interpolation=cv2.INTER_NEAREST,
... 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']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... image=image,
... mask=mask,
... bboxes=bboxes,
... bbox_labels=bbox_labels,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> transformed_image = transformed['image'] # Elastically deformed image
>>> transformed_mask = transformed['mask'] # Elastically deformed mask
>>> transformed_bboxes = transformed['bboxes'] # Elastically deformed bounding boxes
>>> transformed_bbox_labels = transformed['bbox_labels'] # Labels for transformed bboxes
>>> transformed_keypoints = transformed['keypoints'] # Elastically deformed keypoints
>>> transformed_keypoint_labels = transformed['keypoint_labels'] # Labels for transformed keypointsThis transformation is particularly useful for data augmentation in medical imaging and other domains where elastic deformations can simulate realistic variations.