從影像產生 Gabor 濾波器 / 初級視覺皮層「簡單細胞」#

如何在沒有任何複雜的數學運算,僅使用標準 Python 科學函式庫的情況下,為影像分類建立一個 (生物上合理的) *稀疏* 字典 (或「碼簿」或「濾波器組」)?

請在下方找到簡短的答案 ;-)

這個簡單的範例示範如何僅使用簡單的影像來取得類似 Gabor 的濾波器 [1]。在我們的範例中,我們使用太空人 Eileen Collins 的照片。Gabor 濾波器是哺乳動物初級視覺皮層 (V1) 中發現的「簡單細胞」[2] 感受野 [3] 的良好近似值 (詳情請參閱 Hubel & Wiesel 在 60 年代完成的諾貝爾獎得獎研究 [4] [5])。

這裡我們使用 McQueen 的「k-means」演算法 [6],作為一個簡單的生物上合理的類赫布學習規則,並將其應用於 (a) 原始影像的圖塊 (視網膜投射),以及 (b) 使用簡單高斯差分 (DoG) 近似的類 LGN [7] 影像的圖塊。

請盡情享受 ;-) 並請記住,在自然影像圖塊上取得 Gabor 濾波器並非難事。

Image (original), K-means filterbank (codebook) on original image, Image (LGN-like DoG), K-means filterbank (codebook) on LGN-like DoG image
/home/runner/work/scikit-image/scikit-image/doc/examples/features_detection/plot_gabors_from_astronaut.py:57: UserWarning:

One of the clusters is empty. Re-run kmeans with a different initialization.

from scipy.cluster.vq import kmeans2
from scipy import ndimage as ndi
import matplotlib.pyplot as plt

from skimage import data
from skimage import color
from skimage.util.shape import view_as_windows
from skimage.util import montage

patch_shape = 8, 8
n_filters = 49

astro = color.rgb2gray(data.astronaut())

# -- filterbank1 on original image
patches1 = view_as_windows(astro, patch_shape)
patches1 = patches1.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
fb1, _ = kmeans2(patches1, n_filters, minit='points')
fb1 = fb1.reshape((-1,) + patch_shape)
fb1_montage = montage(fb1, rescale_intensity=True)

# -- filterbank2 LGN-like image
astro_dog = ndi.gaussian_filter(astro, 0.5) - ndi.gaussian_filter(astro, 1)
patches2 = view_as_windows(astro_dog, patch_shape)
patches2 = patches2.reshape(-1, patch_shape[0] * patch_shape[1])[::8]
fb2, _ = kmeans2(patches2, n_filters, minit='points')
fb2 = fb2.reshape((-1,) + patch_shape)
fb2_montage = montage(fb2, rescale_intensity=True)

# -- plotting
fig, axes = plt.subplots(2, 2, figsize=(7, 6))
ax = axes.ravel()

ax[0].imshow(astro, cmap=plt.cm.gray)
ax[0].set_title("Image (original)")

ax[1].imshow(fb1_montage, cmap=plt.cm.gray)
ax[1].set_title("K-means filterbank (codebook)\non original image")

ax[2].imshow(astro_dog, cmap=plt.cm.gray)
ax[2].set_title("Image (LGN-like DoG)")

ax[3].imshow(fb2_montage, cmap=plt.cm.gray)
ax[3].set_title("K-means filterbank (codebook)\non LGN-like DoG image")

for a in ax.ravel():
    a.axis('off')

fig.tight_layout()
plt.show()

腳本的總執行時間: (0 分 1.243 秒)

由 Sphinx-Gallery 產生圖庫