注意
前往結尾下載完整範例程式碼。或透過 Binder 在您的瀏覽器中執行此範例
使用簡單的 NumPy 操作來處理影像#
此腳本說明如何使用基本的 NumPy 操作,例如切片、遮罩和花式索引,來修改影像的像素值。

import numpy as np
from skimage import data
import matplotlib.pyplot as plt
camera = data.camera()
camera[:10] = 0
mask = camera < 87
camera[mask] = 255
inds_x = np.arange(len(camera))
inds_y = (4 * inds_x) % len(camera)
camera[inds_x, inds_y] = 0
l_x, l_y = camera.shape[0], camera.shape[1]
X, Y = np.ogrid[:l_x, :l_y]
outer_disk_mask = (X - l_x / 2) ** 2 + (Y - l_y / 2) ** 2 > (l_x / 2) ** 2
camera[outer_disk_mask] = 0
plt.figure(figsize=(4, 4))
plt.imshow(camera, cmap='gray')
plt.axis('off')
plt.show()
腳本的總執行時間: (0 分鐘 0.130 秒)