← Back to all transforms

Crop

Description

Crop a specific region from the input image.

    This transform crops a rectangular region from the input image, mask, bounding boxes, and keypoints
    based on specified coordinates. It's useful when you want to extract a specific area of interest
    from your inputs.

    Args:
        x_min (int): Minimum x-coordinate of the crop region (left edge). Must be >= 0. Default: 0.
        y_min (int): Minimum y-coordinate of the crop region (top edge). Must be >= 0. Default: 0.
        x_max (int): Maximum x-coordinate of the crop region (right edge). Must be > x_min. Default: 1024.
        y_max (int): Maximum y-coordinate of the crop region (bottom edge). Must be > y_min. Default: 1024.
        pad_if_needed (bool): Whether to pad if crop coordinates exceed image dimensions. Default: False.
        pad_mode (OpenCV flag): OpenCV border mode used for padding. Default: cv2.BORDER_CONSTANT.
        pad_cval (number | tuple[number] | list[number]): Padding value if border_mode is cv2.BORDER_CONSTANT.
            Default: 0.
        pad_cval_mask (number | tuple[number] | list[number]): Padding value for masks. Default: 0.
        pad_position (str): Position of padding ('center', 'top_left', 'top_right', 'bottom_left',
            'bottom_right', 'random'). Default: 'center'.
        always_apply (bool, optional): If set to True, the transform will be always applied. Default: None.
        p (float): Probability of applying the transform. Default: 1.0.

    Targets:
        image, mask, bboxes, keypoints

    Image types:
        uint8, float32

    Note:
        - The crop coordinates are applied as follows: x_min <= x < x_max and y_min <= y < y_max.
        - If pad_if_needed is False and crop region extends beyond image boundaries, it will be clipped.
        - If pad_if_needed is True, image will be padded to accommodate the full crop region.
        - For bounding boxes and keypoints, coordinates are adjusted appropriately for both padding and cropping.
    

Parameters

  • x_min: int (default: 100)
  • y_min: int (default: 100)
  • x_max: int (default: 300)
  • y_max: int (default: 500)
  • pad_if_needed: bool (default: false)
  • pad_mode: Literal['cv2.BORDER_CONSTANT', 'cv2.BORDER_REPLICATE', 'cv2.BORDER_REFLECT', 'cv2.BORDER_WRAP', 'cv2.BORDER_DEFAULT', 'cv2.BORDER_TRANSPARENT'] (default: 0)
  • pad_cval: int | float | tuple[int | float, int | float] | list[int | float] (default: 0)
  • pad_cval_mask: int | float | tuple[int | float, int | float] | list[int | float] (default: 0)
  • pad_position: Literal['center', 'top_left', 'top_right', 'bottom_left', 'bottom_right', 'random'] (default: 'center')
  • p: float (default: 1)

Targets

  • Image
  • Mask
  • BBoxes
  • Keypoints

Try it out

Original Image:

Original Image: (733, 484, 3)

Original Image

Bbox Params

Keypoint Params

Mask: (733, 484, 3)

Mask

BBoxes: (733, 484, 3)

BBoxes

Keypoints: (733, 484, 3)

Keypoints