从图像中提取 Gabor 滤波器 / 初级视觉皮层“简单细胞”#

如何构建一个(生物似然的)稀疏字典(或“码本”,或“滤波器组”)用于例如图像分类,而无需任何复杂的数学运算,只需使用标准的 Python 科学库?

请在下面找到一个简短的答案 ;-)

这个简单的例子展示了如何使用简单图像获取类似 Gabor 的滤波器 [1]。在我们的示例中,我们使用了一张宇航员艾琳·科林斯的照片。Gabor 滤波器是哺乳动物初级视觉皮层 (V1) 中发现的“简单细胞” [2] 感受野 [3] 的良好近似(有关详细信息,请参见例如 60 年代由 Hubel 和 Wiesel 进行的诺贝尔奖获奖工作 [4] [5])。

这里我们使用 McQueen 的“kmeans”算法 [6] 作为一种简单的生物似然的 Hebbian 式学习规则,并将它应用于 (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.

/home/runner/work/scikit-image/scikit-image/doc/examples/features_detection/plot_gabors_from_astronaut.py:65: 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 分钟 0.989 秒)

由 Sphinx-Gallery 生成的画廊