AlbumentationsExplore
DocumentationExploreAdoptionPricingBlog
GitHub

XYMasking

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

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.

Arguments
num_masks_x_range
tuple[int, int]
[0, 0]

Range for the number of vertical strips. Defaults to (0, 0).

num_masks_y_range
tuple[int, int]
[0, 0]

Range for the number of horizontal strips. Defaults to (0, 0).

mask_x_length_range
tuple[int | float, int | float]
[0, 0]

Range 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_range
tuple[int | float, int | float]
[0, 0]

Range 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).

fill
float | tuple[float, ...] | random | random_uniform | inpaint_telea | inpaint_ns | grayscale
0

Value 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_mask
tuple[float, ...] | float | None

Fill value for dropout regions in the mask. If None, mask regions corresponding to image dropouts are unchanged. Defaults to None.

p
float
0.5

Probability of applying the transform. Defaults to 0.5.

Examples
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"]
Notes
  • 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_range and mask_y_length_range must have a positive maximum.
  • When using fill="grayscale", fill_mask must be None.