前言
本章,我们开启新的图像处理内容,在均值滤波、中值滤波中均介绍Python和FPGA实现,高斯和双边滤波只做Python的实现,感兴趣的小伙伴可以自己实现。
一、为什么要滤波
在图像传感器成像过程中,光电转换及数模放大时,不可避免的产生噪声。噪声公式如下。
f ( x , y ) = I ( x , y ) + n o i s e f(x, y) = I(x, y) + noise f(x,y)=I(x,y)+noise
二、对图像增加噪音
椒(0)盐(255)噪声,椒是黑色,盐是白色。可以在noise_img = np.where(noise == 0, img, 255).astype(np.uint8)代码中进行更改。保存增噪的图像,用于FPGA的处理。
import numpy as np
import matplotlib.pyplot as plt
img = plt.imread("lenna.png")
img = img * 255#图像是[0-1]--->[0-255],确认一下自己的图像是[0-1]还是[0-255]
img = img.astype(np.uint8)
prob = 0.92#生成盐噪生的概率为1-0.92=0.08
noise = np.random.choice((0, 255), size=img.shape, p=[prob, 1 - prob])#产生和图像维度一样的噪音矩阵
noise_img = np.where(noise == 0, img, 255).astype(np.uint8)#生成噪音图像,椒(0黑色)盐噪声的盐(255白色)
plt.imsave(