Draw a Rectangle with cv2.rectangle

Tutorial

You can use the cv2.rectangle method to draw a rectangle on an image.

This is useful for plotting bounding boxes for use in visualizing predictions from an object detection model.

To draw a rectangle, use the following code:


image = cv2.rectangle(image, (x0, y0), (x1, y1), color=(255, 255, 255))

The cv2.rectangle method requires the x0, y0 coordinates (top left) and x1, y1 coordinates (bottom right) of the box you want to draw.

The method returns an image with the specified rectangle drawn on.

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.