Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Drawing circles on an image with Matplotlib and NumPy
Drawing circles on an image combines image processing with matplotlib's patch functionality. This technique is useful for highlighting regions of interest, marking detected objects, or creating visual annotations.
Basic Circle Drawing
Here's how to draw circles on a sample image using matplotlib patches ?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Read an image from file
img = plt.imread('bird.jpg')
# Generate random positions for circles
x = np.random.rand(5) * img.shape[1]
y = np.random.rand(5) * img.shape[0]
# Create figure and axis
fig, ax = plt.subplots(1)
# Display the image
ax.imshow(img)
ax.axis('off')
# Add circles at random positions
for xx, yy in zip(x, y):
circ = Circle((xx, yy), 50, color='red')
ax.add_patch(circ)
plt.show()
Creating a Synthetic Image Example
Let's create a complete example using a synthetic image that can run without external files ?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
# Create a synthetic image (gradient background)
height, width = 300, 400
img = np.zeros((height, width, 3))
for i in range(height):
for j in range(width):
img[i, j] = [i/height, j/width, 0.5]
# Define circle positions and properties
circle_data = [
(100, 100, 30, 'red'),
(200, 150, 40, 'blue'),
(300, 80, 25, 'green')
]
# Create figure and axis
fig, ax = plt.subplots(figsize=(8, 6))
# Display the synthetic image
ax.imshow(img)
ax.axis('off')
# Add circles with different colors and sizes
for x, y, radius, color in circle_data:
circle = Circle((x, y), radius, color=color, fill=False, linewidth=2)
ax.add_patch(circle)
plt.title('Circles on Synthetic Image')
plt.show()
Circle Properties and Customization
You can customize circles with various properties ?
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
# Create a simple background
img = np.ones((200, 300, 3)) * 0.8
fig, ax = plt.subplots(figsize=(8, 5))
ax.imshow(img)
ax.axis('off')
# Different circle styles
circles = [
Circle((50, 50), 20, color='red', fill=True, alpha=0.7), # Filled with transparency
Circle((150, 50), 20, color='blue', fill=False, linewidth=3), # Outline only
Circle((250, 50), 20, color='green', fill=True), # Solid filled
Circle((50, 150), 20, facecolor='yellow', edgecolor='black', linewidth=2), # Custom colors
Circle((150, 150), 20, color='purple', linestyle='--', fill=False), # Dashed outline
]
for circle in circles:
ax.add_patch(circle)
plt.title('Different Circle Styles')
plt.show()
Key Properties
| Property | Description | Example Values |
|---|---|---|
color |
Circle color | 'red', '#FF5733' |
fill |
Whether to fill the circle | True, False |
alpha |
Transparency level | 0.0 to 1.0 |
linewidth |
Border thickness | 1, 2, 3 |
Conclusion
Drawing circles on images with matplotlib is straightforward using the Circle patch class. Use add_patch() to overlay circles with customizable colors, sizes, and transparency for effective image annotation.
Advertisements
