• AlbumentationsAlbumentations
All TransformsGet LicenseDocumentationNews & Insights
Report IssueJoin Discord...

PiecewiseAffine

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

Apply piecewise affine transformations via a regular grid of control points. Params: scale_range, nb_rows_range, nb_cols_range, interpolation.

This augmentation places a regular grid of points on an image and randomly moves the neighborhood of these points around via affine transformations. This leads to local distortions in the image.

Arguments
scale_range
tuple[float, float]
[0.03,0.05]

Standard deviation of the normal distributions used to sample random corner offsets, sampled per image. Recommended values are in (0.01, 0.05) for small distortions and (0.05, 0.1) for larger distortions. Default: (0.03, 0.05).

nb_rows_range
tuple[int, int]
[4,4]

Range for the number of rows in the regular grid; a value from the discrete interval [a..b] is uniformly sampled per image. Both ends must be >= 2. Default: (4, 4).

nb_cols_range
tuple[int, int]
[4,4]

Range for the number of columns in the regular grid; a value from the discrete interval [a..b] is uniformly sampled per image. Both ends must be >= 2. Default: (4, 4).

interpolation
0 | 1 | 2 | 3 | 4
1

Flag that is used to specify the interpolation algorithm. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_LINEAR.

mask_interpolation
0 | 1 | 2 | 3 | 4
0

Flag 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.

absolute_scale
bool
false

If set to True, the value of the scale parameter will be treated as an absolute pixel value. If set to False, it will be treated as a fraction of the image height and width. Default: False.

keypoint_remapping_method
direct | mask
mask

Method 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"
p
float
0.5

Probability of applying the transform. Default: 0.5.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.Compose([
...     A.PiecewiseAffine(scale_range=(0.03, 0.05), nb_rows_range=(4, 4), nb_cols_range=(4, 4), p=0.5),
... ])
>>> transformed = transform(image=image)
>>> transformed_image = transformed["image"]
Notes
  • This augmentation is very slow. Consider using ElasticTransform instead, which is at least 10x faster.
  • The augmentation may not always produce visible effects, especially with small scale values.
  • For keypoints and bounding boxes, the transformation might move them outside the image boundaries. In such cases, the keypoints will be set to (-1, -1) and the bounding boxes will be removed.