导读
这篇文章主要介绍如何利用opencv来对图像添加各类噪声,原图
高斯噪声
高斯噪声就是给图片添加一个服从高斯分布的噪声 ,可以通过调节高斯分布标准差(sigma) 的大小来控制添加噪声程度,sigma 越大添加的噪声越多图片损坏的越厉害
img = cv2.imread("demo.png")
mean = 0
sigma = 25
gauss = np.random.normal(mean,sigma,(img_height,img_width,img_channels))
noisy_img = image + gauss
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
cv2.imwrite("noisy_img.png",noise_img)
椒盐噪声
椒盐噪声就是给图片添加黑白噪点 ,椒指的是黑色的噪点(0,0,0) 盐指的是白色的噪点(255,255,255) ,通过设置amount 来控制添加噪声的比例,值越大添加的噪声越多,图像损坏的更加严重
img = cv2.imread("demo.png")
s_vs_p = 0.5
amount = 0.04
noisy_img = 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]
noisy_img[coords] = 255
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]
noisy_img[coords] = 0
cv2.imwrite("noisy_img.png",noise_img)
泊松噪声
img = cv2.imread("demo.png")
vals = len(np.unique(image))
vals = 2 ** np.ceil(np.log2(vals))
noisy_img = np.random.poisson(image * vals) / float(vals)
cv2.imwrite("noisy_img.png",noise_img)
speckle噪声
img = cv2.imread("demo.png")
gauss = np.random.randn(img_height,img_width,img_channels)
noisy_img = image + image * gauss
noisy_img = np.clip(noisy_img,a_min=0,a_max=255)
cv2.imwrite("noisy_img.png",noise_img)
|