卷积核(5*5的卷积核中每个值均为1)是用当前像素跟它邻域内的像素取平均,这样可以使图像上噪声比较大的点变得更平滑,如下代码所示:

import paddle
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from paddle.nn import Conv2D
from paddle.nn.initializer import Assign

img = Image.open('work/000000355610.jpg').convert('L')
img = np.array(img)

# 创建初始化参数
w = np.ones([1, 1, 5, 5], dtype='float32')/25
conv = Conv2D(in_channels=1, out_channels=1, kernel_size=[5, 5],
              weight_attr=paddle.ParamAttr(initializer=Assign(value=w)))
x = img.astype('float32')
x = x.reshape(1, 1, img.shape[0], img.shape[1])
x = paddle.to_tensor(x)
y = conv(x)
out = y.numpy()

plt.figure(figsize=(20, 12))
f = plt.subplot(121)
f.set_title('input image')
plt.imshow(img, cmap='gray')

f = plt.subplot(122)
f.set_title('output feature map')
plt.imshow(out.squeeze(), cmap='gray')
plt.show()

转载请注明来源:https://www.longjin666.top/?p=1030

欢迎关注我的公众号“灯珑”,让我们一起了解更多的事物~

你也可能喜欢

发表评论