Crop images with cv2

Tutorial

OpenCV images are represented as three-dimensional NumPy arrays. Thus, you can use NumPy slicing to crop images with cv2.

To crop an image, you will need the crop region x starting point, y starting point, width, and height.

You can crop an image using the following code:


import numpy as np
import cv2

image = cv2.imread('download.jpg')
x = 0
y = 0
width = 0
height = 0
cropped_image = image[y:y + height, x:x + width]

Next Steps

OpenCV can be used with the open source supervision Python package.

supervision provides an extensive range of functionalities for working with computer vision models. With supervision, you can:

1. Process and filter detections and segmentation masks from a range of popular models (YOLOv5, Ultralytics YOLOv8, MMDetection, and more).
2. Process and filter classifications.
3. Compute confusion matrices.

And more! To learn about the full range of functionality in supervision, check out the supervision documentation.