cv3.transform.crop

cv3.transform.crop(img, x0, y0, x1, y1, mode='xyxy', rel=None, copy=True)[source]

Crop image to specified rectangle.

Parameters:
  • img (numpy.ndarray) – Input image.

  • x0 (int or float) – Rectangle coordinates.

  • y0 (int or float) – Rectangle coordinates.

  • x1 (int or float) – Rectangle coordinates.

  • y1 (int or float) – Rectangle coordinates.

  • mode (str, optional) – Coordinate mode. Can be ‘xyxy’, ‘xywh’, ‘ccwh’. Defaults to ‘xyxy’.

  • rel (bool, optional) – Whether to interpret coordinates as relative values. Defaults to None.

  • copy (bool, optional) – Whether to return a copy of the cropped region. Defaults to True.

Returns:

Cropped image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[:, :] = [255, 255, 255]  # White image
>>> # Crop to 50x50 square in the center
>>> cropped = cv3.crop(img, 25, 25, 75, 75)
>>> # Crop without copying the data
>>> cropped_view = cv3.crop(img, 25, 25, 75, 75, copy=False)