การวาดวงกลม (Circle) ทับบนภาพ (Image) ใน Matplotlib ใช้ Class Circle จาก matplotlib.patches ร่วมกับ imshow() แสดงภาพพื้นหลัง
ขั้นตอน
- อ่านภาพด้วย
plt.imread()หรือcv2.imread() - สร้าง Figure และ Axes
- แสดงภาพด้วย
ax.imshow() - สร้าง
CircleObject พร้อมตำแหน่ง รัศมี สี - เพิ่ม Patch ลง Axes ด้วย
ax.add_patch()
ตัวอย่างโค้ด
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
plt.rcParams["figure.figsize"] = [12, 8]
plt.rcParams["figure.autolayout"] = True
# สร้างภาพตัวอย่าง (ถ้าไม่มีไฟล์)
# img = plt.imread('your_image.jpg')
img = np.random.rand(400, 600, 3) # Random RGB Image
# เพิ่มรูปร่างบนภาพเพื่อให้เห็นชัด
img[100:150, 200:300] = [1, 0, 0] # Red rectangle
img[200:250, 400:500] = [0, 1, 0] # Green rectangle
fig, ax = plt.subplots()
# แสดงภาพ
ax.imshow(img)
ax.axis('off') # ซ่อนแกน
# วาดวงกลมสุ่ม 5 วง
np.random.seed(42)
n_circles = 5
for i in range(n_circles):
# ตำแหน่งสุ่ม (ในขอบเขตภาพ)
cx = np.random.uniform(50, img.shape[1] - 50)
cy = np.random.uniform(50, img.shape[0] - 50)
radius = np.random.uniform(20, 60)
# สร้าง Circle Patch
circle = Circle((cx, cy), radius,
edgecolor='yellow', # สีขอบ
facecolor='none', # ใสด้านใน
linewidth=3, # ความหนาขอบ
linestyle='--', # ขอบประกบ
alpha=0.9,
zorder=10) # อยู่บนภาพ
ax.add_patch(circle)
# เพิ่มหมายเลขวงกลม
ax.text(cx, cy, str(i+1), color='yellow', fontsize=14,
fontweight='bold', ha='center', va='center', zorder=11)
ax.set_title('Random Circles on Image', fontsize=16, pad=20)
plt.show()
ตัวอย่างโต้ตอบ: คลิกเพื่อวาดวงกลม
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
img = np.random.rand(400, 600, 3)
fig, ax = plt.subplots()
ax.imshow(img)
ax.axis('off')
ax.set_title('Click to Draw Circles (Right-click to Exit)', fontsize=14)
circles = []
def on_click(event):
if event.inaxes != ax:
return
if event.button == 1: # Left Click
cx, cy = event.xdata, event.ydata
radius = 30
circle = Circle((cx, cy), radius,
edgecolor='lime', facecolor='none',
linewidth=2, alpha=0.8, zorder=10)
ax.add_patch(circle)
circles.append(circle)
fig.canvas.draw()
elif event.button == 3: # Right Click
plt.close(fig)
fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()
ผลลัพธ์
ภาพพื้นหลังพร้อมวงกลมสีเหลืองขอบประกบ พร้อมหมายเลขลำดับอยู่ตรงกลางวงกลม
พารามิเตอร์ Circle สำคัญ
| พารามิเตอร์ | คำอธิบาย |
|---|---|
xy / center | ตำแหน่งศูนย์กลาง (x, y) ใน Data Coordinates |
radius | รัศมี (Data Units) |
facecolor / fc | สีภายใน ('none' = ใส) |
edgecolor / ec | สีขอบ |
linewidth / lw | ความหนาขอบ |
linestyle / ls | รูปแบบขอบ ('-', '--', '-.', ':') |
fill | True/False - เติมสีหรือไม่ |
alpha | ความโปร่งใส |
zorder | ลำดับชั้นการวาด (มาก = บน) |
Patch อื่นๆ ที่น่าสนใจ
from matplotlib.patches import Rectangle, Ellipse, Polygon, FancyBboxPatch, Arrow # Rectangle rect = Rectangle((x, y), width, height, angle=0, ...) # Ellipse (วงรี) ellipse = Ellipse((cx, cy), width, height, angle=45, ...) # Polygon (หลายเหลี่ยม) poly = Polygon([(x1,y1), (x2,y2), (x3,y3)], ...) # Rounded Rectangle fancy = FancyBboxPatch((x, y), width, height, boxstyle="round,pad=0.3", ...)