Last modified: Jan 15, 2025 By Alexander Williams

Python OpenCV cv2.rectangle() Guide

OpenCV is a powerful library for image processing. One of its key functions is cv2.rectangle(). This function allows you to draw rectangles on images. It is useful for highlighting areas of interest.

In this guide, we will explore how to use cv2.rectangle() in Python. We will cover the syntax, parameters, and provide examples. By the end, you will be able to draw rectangles on images with ease.

What is cv2.rectangle()?

The cv2.rectangle() function is used to draw rectangles on images. It requires the image, the coordinates of the rectangle, the color, and the thickness of the rectangle's border.

This function is often used in object detection, image annotation, and other computer vision tasks. It helps in visualizing the detected objects or regions in an image.

Syntax of cv2.rectangle()

The syntax of cv2.rectangle() is straightforward. Here is the basic structure:


cv2.rectangle(image, start_point, end_point, color, thickness)

Parameters:

  • image: The image on which the rectangle will be drawn.
  • start_point: The top-left corner of the rectangle (x, y).
  • end_point: The bottom-right corner of the rectangle (x, y).
  • color: The color of the rectangle in BGR format (e.g., (255, 0, 0) for blue).
  • thickness: The thickness of the rectangle's border. Use -1 to fill the rectangle.

Example: Drawing a Rectangle on an Image

Let's see how to use cv2.rectangle() in practice. We will draw a rectangle on an image and display it.


import cv2

# Load an image
image = cv2.imread('image.jpg')

# Define the rectangle's start and end points
start_point = (50, 50)
end_point = (200, 200)

# Define the color (BGR format) and thickness
color = (0, 255, 0)  # Green
thickness = 2

# Draw the rectangle on the image
cv2.rectangle(image, start_point, end_point, color, thickness)

# Display the image
cv2.imshow('Rectangle on Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we load an image using cv2.imread(). We then define the rectangle's top-left and bottom-right corners. The rectangle is drawn in green with a thickness of 2 pixels. Finally, the image is displayed using cv2.imshow().

Filling a Rectangle

If you want to fill the rectangle with color, set the thickness parameter to -1. Here's how you can do it:


import cv2

# Load an image
image = cv2.imread('image.jpg')

# Define the rectangle's start and end points
start_point = (50, 50)
end_point = (200, 200)

# Define the color (BGR format) and thickness
color = (0, 0, 255)  # Red
thickness = -1  # Fill the rectangle

# Draw the rectangle on the image
cv2.rectangle(image, start_point, end_point, color, thickness)

# Display the image
cv2.imshow('Filled Rectangle', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, the rectangle is filled with red color. The thickness is set to -1, which fills the rectangle completely.

Combining cv2.rectangle() with Other Functions

You can combine cv2.rectangle() with other OpenCV functions for more advanced tasks. For example, you can use cv2.findContours() to detect objects and then draw rectangles around them.

Here's an example where we detect edges using cv2.Canny(), find contours, and draw rectangles around the detected objects:


import cv2

# Load an image
image = cv2.imread('image.jpg')

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply edge detection
edges = cv2.Canny(gray, 100, 200)

# Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw rectangles around the detected objects
for contour in contours:
    x, y, w, h = cv2.boundingRect(contour)
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Display the image
cv2.imshow('Detected Objects', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we first convert the image to grayscale using cv2.cvtColor(). Then, we apply edge detection using cv2.Canny(). After finding contours with cv2.findContours(), we draw rectangles around the detected objects.

Conclusion

The cv2.rectangle() function is a simple yet powerful tool in OpenCV. It allows you to draw rectangles on images, which is useful for various computer vision tasks. Whether you're highlighting regions of interest or visualizing detected objects, cv2.rectangle() is an essential function to know.

By combining cv2.rectangle() with other OpenCV functions like cv2.findContours() or cv2.Canny(), you can create more advanced image processing pipelines. Experiment with different parameters and see how you can use this function in your projects.

For more information on related OpenCV functions, check out our guides on cv2.findContours() and cv2.Canny().