4.1 添加随机噪点
img = Image.open('1.jpg')
img = img.resize((640, 640))
img_tensor = torch.tensor(np.array(img), dtype=torch.float).permute(2, 0, 1).unsqueeze(0)
noise = torch.randn_like(img_tensor)
noisy_img_tensor = img_tensor + noise
noisy_img = noisy_img_tensor.squeeze(0).permute(1, 2, 0).to(dtype=torch.uint8)
noisy_img = Image.fromarray(noisy_img.numpy())
4.2 图像去噪
font = FontProperties(fname='C:\Windows\Fonts\simkai.ttf', size=32)
plt.figure(figsize=(32, 32))
plt.subplot(1, 3, 1)
plt.title('原始图像', fontproperties=font)
plt.imshow(img)
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title('噪点图像', fontproperties=font)
plt.imshow(noisy_img)
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title('去噪图像', fontproperties=font)
plt.imshow(conv2d(noisy_img_tensor, conv_kernel1))
plt.axis('off')
plt.show()