XYMasking
Mask images with random horizontal and vertical strips sized in pixels or axis-relative fractions, useful for occlusion and spectrogram augmentation.
Integer endpoints express strip sizes in pixels. Float endpoints in [0.0, 1.0] scale X lengths by image width and Y lengths by image height.
num_masks_x_rangeRange for the number of vertical strips. Defaults to (0, 0).
num_masks_y_rangeRange for the number of horizontal strips. Defaults to (0, 0).
mask_x_length_rangeRange of vertical-strip widths. Use two integers for pixels or two floats in [0.0, 1.0] for fractions of image width. Defaults to (0, 0).
mask_y_length_rangeRange of horizontal-strip heights. Use two integers for pixels or two floats in [0.0, 1.0] for fractions of image height. Defaults to (0, 0).
fillValue for the dropped pixels. Can be:
- int or float: all channels are filled with this value
- tuple: tuple of values for each channel
- 'random': each pixel is filled with random values
- 'random_uniform': each hole is filled with a single random color
- 'inpaint_telea': uses OpenCV Telea inpainting method
- 'inpaint_ns': uses OpenCV Navier-Stokes inpainting method
- 'grayscale': converts dropped regions to grayscale while preserving channel count Default: 0
fill_maskFill value for dropout regions in the mask. If None, mask regions corresponding to image dropouts are unchanged. Defaults to None.
pProbability of applying the transform. Defaults to 0.5.
Use integer ranges for pixel-based strip sizes:
>>> import albumentations as A
>>> import numpy as np
>>> image = np.full((80, 120, 3), 255, dtype=np.uint8)
>>> pixel_transform = A.XYMasking(
... num_masks_x_range=(1, 2),
... num_masks_y_range=(1, 1),
... mask_x_length_range=(8, 16),
... mask_y_length_range=(4, 8),
... fill=0,
... p=1.0,
... )
>>> pixel_image = pixel_transform(image=image)["image"]- Each length range must contain either two integers or two floats; mixed endpoint types are invalid.
- Integer lengths are sampled inclusively in pixels. Float lengths are sampled between the endpoints, scaled by the corresponding axis, and rounded down. Zero-length strips are omitted, and 1.0 can span the full axis.
- At least one of
mask_x_length_rangeandmask_y_length_rangemust have a positive maximum. - When using
fill="grayscale",fill_maskmust be None.