RandomCrop3D
Crop random part of 3D volume.
Supported Targets
keypoints
Arguments
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, int, int] | Desired output size of the crop in format (depth, height, width) | |
pad_if_needed | bool | Whether to pad if the volume is smaller than desired crop size. Default: False | |
fill | tuple[float, float] | float | Padding value for image if pad_if_needed is True. Default: 0 | |
fill_mask | tuple[float, float] | float | Padding value for mask if pad_if_needed is True. Default: 0 | |
p | float | probability of applying the transform. Default: 1.0 |
Image Types
uint8, float32
Notes
If you want to perform random cropping only in the XY plane while preserving all slices along
the Z axis, consider using RandomCrop instead. RandomCrop will apply the same XY crop
to each slice independently, maintaining the full depth of the volume.
Examples
>>> import numpy as np
>>> import albumentations as A
>>>
>>> # Prepare sample data
>>> volume = np.random.randint(0, 256, (20, 200, 200), dtype=np.uint8) # (D, H, W)
>>> mask3d = np.random.randint(0, 2, (20, 200, 200), dtype=np.uint8) # (D, H, W)
>>> keypoints = np.array([[100, 100, 10], [150, 150, 15]], dtype=np.float32) # (x, y, z)
>>> keypoint_labels = [1, 2] # Labels for each keypoint
>>>
>>> # Create the transform with random crop and padding if needed
>>> transform = A.Compose([
... A.RandomCrop3D(
... size=(16, 128, 128), # Output size (depth, height, width)
... pad_if_needed=True, # Pad if input is smaller than crop size
... fill=0, # Fill value for volume padding
... fill_mask=1, # Fill value for mask padding
... p=1.0
... )
... ], keypoint_params=A.KeypointParams(format='xyz', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> transformed = transform(
... volume=volume,
... mask3d=mask3d,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the transformed data
>>> cropped_volume = transformed["volume"] # Shape: (16, 128, 128)
>>> cropped_mask3d = transformed["mask3d"] # Shape: (16, 128, 128)
>>> cropped_keypoints = transformed["keypoints"] # Keypoints shifted relative to random crop
>>> cropped_keypoint_labels = transformed["keypoint_labels"] # Labels remain unchanged