← Back to all transforms
PadIfNeeded
Description
Pads the sides of an image if the image dimensions are less than the specified minimum dimensions. If the `pad_height_divisor` or `pad_width_divisor` is specified, the function additionally ensures that the image dimensions are divisible by these values. Args: min_height (int | None): Minimum desired height of the image. Ensures image height is at least this value. If not specified, pad_height_divisor must be provided. min_width (int | None): Minimum desired width of the image. Ensures image width is at least this value. If not specified, pad_width_divisor must be provided. pad_height_divisor (int | None): If set, pads the image height to make it divisible by this value. If not specified, min_height must be provided. pad_width_divisor (int | None): If set, pads the image width to make it divisible by this value. If not specified, min_width must be provided. position (Literal["center", "top_left", "top_right", "bottom_left", "bottom_right", "random"]): Position where the image is to be placed after padding. Default is 'center'. border_mode (int): Specifies the border mode to use if padding is required. The default is `cv2.BORDER_REFLECT_101`. value (int, float, list[int], list[float] | None): Value to fill the border pixels if the border mode is `cv2.BORDER_CONSTANT`. Default is None. mask_value (int, float, list[int], list[float] | None): Similar to `value` but used for padding masks. Default is None. p (float): Probability of applying the transform. Default is 1.0. Targets: image, mask, bboxes, keypoints Image types: uint8, float32 Note: - Either `min_height` or `pad_height_divisor` must be set, but not both. - Either `min_width` or `pad_width_divisor` must be set, but not both. - If `border_mode` is set to `cv2.BORDER_CONSTANT`, `value` must be provided. - The transform will maintain consistency across all targets (image, mask, bboxes, keypoints). - For bounding boxes, the coordinates will be adjusted to account for the padding. - For keypoints, their positions will be shifted according to the padding. Example: >>> import albumentations as A >>> transform = A.Compose([ ... A.PadIfNeeded(min_height=1024, min_width=1024, border_mode=cv2.BORDER_CONSTANT, value=0), ... ]) >>> transformed = transform(image=image, mask=mask, bboxes=bboxes, keypoints=keypoints) >>> padded_image = transformed['image'] >>> padded_mask = transformed['mask'] >>> adjusted_bboxes = transformed['bboxes'] >>> adjusted_keypoints = transformed['keypoints']
Parameters
- min_height: int | None (default: 1024)
- min_width: int | None (default: 1024)
- pad_height_divisor: int | None (default: null)
- pad_width_divisor: int | None (default: null)
- position: Literal['center', 'top_left', 'top_right', 'bottom_left', 'bottom_right', 'random'] (default: 'center')
- border_mode: Literal['cv2.BORDER_CONSTANT', 'cv2.BORDER_REPLICATE', 'cv2.BORDER_REFLECT', 'cv2.BORDER_WRAP', 'cv2.BORDER_DEFAULT', 'cv2.BORDER_TRANSPARENT'] (default: 4)
- value: float | Sequence[float] | None (default: null)
- mask_value: float | Sequence[float] | None (default: null)
- p: float (default: 1)
Targets
- Image
- Mask
- BBoxes
- Keypoints
Try it out
ⓘ