← Back to all transforms
CropNonEmptyMaskIfExists
Description
Crop area with mask if mask is non-empty, else make random crop. This transform attempts to crop a region containing a mask (non-zero pixels). If the mask is empty or not provided, it falls back to a random crop. This is particularly useful for segmentation tasks where you want to focus on regions of interest defined by the mask. Args: height (int): Vertical size of crop in pixels. Must be > 0. width (int): Horizontal size of crop in pixels. Must be > 0. ignore_values (list of int, optional): Values to ignore in mask, `0` values are always ignored. For example, if background value is 5, set `ignore_values=[5]` to ignore it. Default: None. ignore_channels (list of int, optional): Channels to ignore in mask. For example, if background is the first channel, set `ignore_channels=[0]` to ignore it. Default: None. p (float): Probability of applying the transform. Default: 1.0. Targets: image, mask, bboxes, keypoints Image types: uint8, float32 Note: - If a mask is provided, the transform will try to crop an area containing non-zero (or non-ignored) pixels. - If no suitable area is found in the mask or no mask is provided, it will perform a random crop. - The crop size (height, width) must not exceed the original image dimensions. - Bounding boxes and keypoints are also cropped along with the image and mask. Raises: ValueError: If the specified crop size is larger than the input image dimensions. Example: >>> import numpy as np >>> import albumentations as A >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) >>> mask = np.zeros((100, 100), dtype=np.uint8) >>> mask[25:75, 25:75] = 1 # Create a non-empty region in the mask >>> transform = A.Compose([ ... A.CropNonEmptyMaskIfExists(height=50, width=50, p=1.0), ... ]) >>> transformed = transform(image=image, mask=mask) >>> transformed_image = transformed['image'] >>> transformed_mask = transformed['mask'] # The resulting crop will likely include part of the non-zero region in the mask
Parameters
- height: int (default: 500)
- width: int (default: 300)
- ignore_values: list[int] | None (default: null)
- ignore_channels: list[int] | None (default: null)
- p: float (default: 1)
Targets
- Image
- Mask
- BBoxes
- Keypoints
Try it out
ⓘ