Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Overlay Image Segmentation ด้วย NumPy และ Matplotlib


ภาพรวม

สาธิตการวาง Mask (Segmentation) ทับภาพต้นฉบับด้วย Alpha Blending

ขั้นตอน

  1. สร้าง Mask 10x10 มีค่า 1 ในพื้นที่กลาง
  2. สร้างภาพสุ่ม + Noise
  3. Mask พื้นที่ที่ Mask=0 ด้วย np.ma.masked_where
  4. พลอตภาพต้นฉบับ (ซ้าย) และภาพทับ Mask (ขวา)
  5. ใช้ alpha=0.7 และ Colormap 'jet' สำหรับ Mask

ตัวอย่างโค้ด

from matplotlib import pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

mask = np.zeros((10, 10))
mask[3:-3, 3:-3] = 1

im = mask + np.random.randn(10, 10) * 0.01
masked = np.ma.masked_where(mask == 0, mask)

plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(im, 'gray', interpolation='none')
plt.title("Original")

plt.subplot(1, 2, 2)
plt.imshow(im, 'gray', interpolation='none')
plt.imshow(masked, 'jet', interpolation='none', alpha=0.7)
plt.title("Overlay Segmentation")

plt.show()

ผลลัพธ์

ซ้าย: ภาพ Grayscale ขวา: ภาพเดียวกันทับ Mask สีแดง/ส้ม (Jet) โปร่งใส 70%

การประยุกต์

  • Medical Imaging: ทับ Segmented Tumor
  • Computer Vision: Visualize Detection Masks
  • ใช้ cmap='Reds' หรือ Custom Colormap สำหรับ Binary Mask