数字图像的随机噪声在图像处理中有着重要的位置，今天用到了，就回顾一下。做个总结。

随机噪声很多种，最常用的一般有两种，高斯噪声和椒盐噪声，下面我们就针对这两种噪声做个科普。

```我们这里使用numpy即可

gauss = np.random.normal(mean,sigma,(row,col,ch))

def GaussieNoisy(image,sigma):
row,col,ch= image.shape
mean = 0
gauss = np.random.normal(mean,sigma,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = image + gauss
return noisy.astype(np.uint8)```

图像结果：

```def spNoisy(image,s_vs_p = 0.5,amount = 0.004):
row,col,ch = image.shape

out = np.copy(image)
num_salt = np.ceil(amount * image.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt))  for i in image.shape]
out[coords] = 1
num_pepper = np.ceil(amount* image.size * (1. - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
out[coords] = 0
return out```

图片效果：

总体代码：

```import cv2
import numpy as np
import matplotlib.pyplot as plt
import scipy
import scipy.stats

def GaussieNoisy(image,sigma):
row,col,ch= image.shape
mean = 0
gauss = np.random.normal(mean,sigma,(row,col,ch))
gauss = gauss.reshape(row,col,ch)
noisy = image + gauss
return noisy.astype(np.uint8)

def spNoisy(image,s_vs_p = 0.5,amount = 0.004):
row,col,ch = image.shape

out = np.copy(image)
num_salt = np.ceil(amount * image.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt))  for i in image.shape]
out[coords] = 1
#num_pepper = np.ceil(amount * image.size * (2. - s_vs_p))
num_pepper = np.ceil(amount * image.size * (1 - 0.5))
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
out[coords] = 0
return out