python使用opencv对图像添加(高斯/椒盐/泊松/斑点)噪声 | 您所在的位置:网站首页 › 生成各种程度的含噪图像 › python使用opencv对图像添加(高斯/椒盐/泊松/斑点)噪声 |
导读
这篇文章主要介绍如何利用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) #添加salt噪声 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[0],coords[1],:] = [255,255,255] #添加pepper噪声 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],coords[1],:] = [0,0,0] #保存图片 cv2.imwrite("noisy_img.png",noise_img) |
CopyRight 2018-2019 实验室设备网 版权所有 |