<jupyter_start><jupyter_text>高斯滤波器  在这个例子中，我们对一张图像应用高斯滤波器。高斯函数在 0 周围形成了一个平滑对称的山丘形状。这种滤波器可以生成附近函数值的加权平均。 当应用在图像上时， 它的效果是模糊附近的像素值。 二维高斯滤波器定义如下:![Gaussian Filter](../machine_learning/graph/demo_fe_08_2d_gaussian_filter.png) 其中 σ 是高斯函数的标准差，它可以控制“山丘”的宽度。<jupyter_code>import matplotlib.pyplot as plt
# %matplotlib notebook
%matplotlib inline

import numpy as np
from skimage import data, color
from scipy import signal

## Load example image, and change to Gray image

image = color.rgb2gray(data.chelsea())   ## a cat
# image = color.rgb2gray(data.coffee())    ## a coffee cup

print(image.shape)
print(image[:5])
ind = [-1., -0.5, 0., 0.5, 1.]
X,Y = np.meshgrid(ind, ind)
X
Y
# G is the unnormalized Gaussian kernel where the value at (0,0) is 1.0
G = np.exp(-(np.multiply(X,X) + np.multiply(Y,Y))/2)
G
blurred_image = signal.convolve2d(image, G, mode='valid')
print(blurred_image.shape)
print(blurred_image[:5])
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')
# ax1.set_adjustable('box-forced')
ax1.set_adjustable('box')

ax2.axis('off')
ax2.imshow(blurred_image, cmap=plt.cm.gray)
ax2.set_title('After convolving with a Gaussian filter')
# ax2.set_adjustable('box-forced')
ax2.set_adjustable('box')

fig.savefig('../machine_learning/graph/fe_08_3_gaussian_filter_cat.png')
# fig.savefig('../machine_learning/graph/fe_08_3_gaussian_filter_coffee.png')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup