How to Draw Rectangle on Image in Matplotlib?
0 1539
Drawing Rectangles on Images Using Matplotlib in Python
Matplotlib, a versatile plotting library in Python, offers robust tools for visualizing and annotating images. One such feature is the ability to draw rectangles on images, which is particularly useful for highlighting regions of interest or marking specific areas within an image. In this guide, we'll explore how to draw rectangles on images using Matplotlib's patches.Rectangle function.
Prerequisites
Before we begin, ensure you have the necessary libraries installed:
pip install matplotlib pillow numpy
We'll be using Matplotlib for plotting, Pillow (PIL) for image handling, and NumPy for array manipulation.
Loading and Displaying an Image
To start, we need to load an image and display it using Matplotlib:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np
# Load the image
image = np.array(Image.open('path_to_your_image.jpg'), dtype=np.uint8)
# Display the image
fig, ax = plt.subplots()
ax.imshow(image)
ax.axis('off') # Hide axes for better visualization
plt.show()
Replace 'path_to_your_image.jpg' with the actual path to your image file. The imshow() function renders the image, and axis('off') removes the axis labels for a cleaner presentation.
Drawing a Rectangle on the Image
Now, let's draw a rectangle on the image. We'll use the patches.Rectangle function to create a rectangle and add it to the axes:
# Define the rectangle's parameters
x, y = 50, 100 # Bottom-left corner coordinates
width, height = 40, 30 # Width and height of the rectangle
# Create a Rectangle patch
rect = patches.Rectangle((x, y), width, height, linewidth=1, edgecolor='r', facecolor='none')
# Add the rectangle to the axes
ax.add_patch(rect)
# Display the image with the rectangle
plt.show()
This code will display the image with a red rectangle drawn at the specified coordinates. The linewidth parameter controls the thickness of the rectangle's border, edgecolor sets the color of the border, and facecolor determines the fill color (set to 'none' for transparency).
Drawing a Filled Rectangle
If you prefer a filled rectangle, simply change the facecolor parameter:
# Create a filled Rectangle patch
rect_filled = patches.Rectangle((x, y), width, height, linewidth=1, edgecolor='r', facecolor='g')
# Add the filled rectangle to the axes
ax.add_patch(rect_filled)
# Display the image with the filled rectangle
plt.show()
In this example, the rectangle is filled with a green color. Adjust the facecolor to any valid color name or RGB tuple to customize the fill color.
Rotating the Rectangle
To rotate the rectangle, use the angle parameter in the patches.Rectangle function:
# Create a rotated Rectangle patch
rect_rotated = patches.Rectangle((x, y), width, height, angle=45, linewidth=1, edgecolor='r', facecolor='none')
# Add the rotated rectangle to the axes
ax.add_patch(rect_rotated)
# Display the image with the rotated rectangle
plt.show()
This code rotates the rectangle by 45 degrees. You can adjust the angle to achieve the desired rotation.
Conclusion
Drawing rectangles on images using Matplotlib is a straightforward process that enhances your ability to annotate and highlight specific areas within images. Whether you're marking regions of interest for analysis or creating visual annotations for presentations, Matplotlib's patches.Rectangle function provides a flexible and powerful tool for image annotation tasks.
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:


Comments
Waiting for your comments