第一种方法
def salt_pepper_noise(image, prob):
"""
添加椒盐噪声
:param image: 输入图像
:param prob: 噪声比
:return: 带有椒盐噪声的图像
"""
salt = np.zeros(image.shape, np.uint8)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = np.random.rand()
if rdn < prob:
salt[i][j] = 0
elif rdn > thres:
salt[i][j] = 255
else:
salt[i][j] = image[i][j]
return salt
————————————————
版权声明:本文为CSDN博主「点云侠」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36686437/article/details/120591384
第二种方法
def add_peppersalt_noise(image, n=10000):
result = image.copy()
# 测量图片的长和宽
w, h =image.shape[:2]
# 生成n个椒盐噪声
for i in range(n):
x = np.random.randint(1, w)
y= np.random.randint(1, h)
if np.random.randint(0, 2) == 0 :
result[x, y] = 0
else:
result[x,y] = 255
return result
————————————————
版权声明:本文为CSDN博主「Justth.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_49478668/article/details/123342188
def fast_salt_pepper_noise(image: np.ndarray, prob=0.02):
"""
随机生成一个0~1的mask,作为椒盐噪声
:param image:图像
:param prob: 椒盐噪声噪声比例
:return:
"""
image = add_uniform_noise(image, prob * 0.51, vaule=255)
image = add_uniform_noise(image, prob * 0.5, vaule=0)
return image
def add_uniform_noise(image: np.ndarray, prob=0.05, vaule=255):
"""
随机生成一个0~1的mask,作为椒盐噪声
:param image:图像
:param prob: 噪声比例
:param vaule: 噪声值
:return:
"""
h, w = image.shape[:2]
noise = np.random.uniform(low=0.0, high=1.0, size=(h, w)).astype(dtype=np.float32) # 产生高斯噪声
mask = np.zeros(shape=(h, w), dtype=np.uint8) + vaule
index = noise > prob
mask = mask * (~index)
output = image * index[:, :, np.newaxis] + mask[:, :, np.newaxis]
output = np.clip(output, 0, 255)
output = np.uint8(output)
return output
快速椒盐噪声
添加高斯噪声
def gasuss_noise(image, mu=0.0, sigma=0.1):
"""
添加高斯噪声
:param image: 输入的图像
:param mu: 均值
:param sigma: 标准差
:return: 含有高斯噪声的图像
"""
image = np.array(image / 255, dtype=float)
noise = np.random.normal(mu, sigma, image.shape)
gauss_noise = image + noise
if gauss_noise.min() < 0:
low_clip = -1.
else:
low_clip = 0.
gauss_noise = np.clip(gauss_noise, low_clip, 1.0)
gauss_noise = np.uint8(gauss_noise * 255)
return gauss_noise
————————————————
版权声明:本文为CSDN博主「点云侠」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36686437/article/details/120632210