备注
转到结尾 以下载完整示例代码。或者通过 Binder 在浏览器中运行此示例。
图像中的Gabor滤波器 / 初级视觉皮层“简单细胞”#
如何在没有复杂数学和仅使用标准Python科学库的情况下,构建一个(生物合理性)*稀疏*字典(或’码本’,或’滤波器组’)用于例如图像分类?
请在下方找到简短答案 ;-)
这个简单的例子展示了如何使用一张简单的图像来获取类似Gabor的滤波器 [1] 。在我们的例子中,我们使用了一张宇航员Eileen Collins的照片。Gabor滤波器是对哺乳动物初级视觉皮层(V1)中发现的“简单细胞” [2] 感受野 [3] 的良好近似(有关详细信息,请参见例如Hubel & Wiesel在60年代进行的诺贝尔奖获奖工作 [4] [5])。
在这里,我们使用McQueen的’kmeans’算法 [6],作为一种简单的生物学上合理的类Hebbian学习规则,并将其应用于(a)原始图像的补丁(视网膜投影),以及(b)使用简单的高斯差分(DoG)近似的类LGN [7] 图像的补丁。
享受 ;-) 并记住,在自然图像块上获取Gabor滤波器并不是火箭科学。
/Users/cw/baidu/code/fin_tool/github/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.
/Users/cw/baidu/code/fin_tool/github/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.467 秒)