给图像添加噪声的方法(补充ing)
Posted stateless
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给图像添加噪声的方法(补充ing)相关的知识,希望对你有一定的参考价值。
1.随机修改一部分像素点的灰度值为指定值
def noise(img,proportion=0.05): \'\'\' 随机的修改一定数量像素点的灰度值 :param img: :param proportion: 噪声点占全部像素点的比例 :return: \'\'\' height,width =img.shape[:2] num = int(height*width*proportion)#多少个像素点添加噪声 for k in range(0, num): # get the random point xi = int(np.random.uniform(0, img.shape[1])) xj = int(np.random.uniform(0, img.shape[0])) # add noise if img.ndim == 2: img[xj, xi] = 255 elif img.ndim == 3: img[xj, xi, 0] = 25 img[xj, xi, 1] = 20 img[xj, xi, 2] = 20 return img
2.椒盐噪声
def salt_and_pepper_noise(img, proportion=0.05): \'\'\' :param img: :param proportion: 噪声点占全部像素点的比例 :return: \'\'\' noise_img =img height,width =noise_img.shape[0],noise_img.shape[1] num = int(height*width*proportion)#多少个像素点添加椒盐噪声 for i in range(num): w = random.randint(0,width-1) #随机图片加噪声的位置 h = random.randint(0,height-1) if random.randint(0,1) ==0: noise_img[h,w] =0 #加黑点 else: noise_img[h,w] = 255 #加白点 return noise_img
3,高斯噪声
以上是关于给图像添加噪声的方法(补充ing)的主要内容,如果未能解决你的问题,请参考以下文章
给图像添加椒盐噪声后用均值滤波和中值滤波过滤椒盐噪声的C++-OpenCV代码
如何给图片添加泊松(poisson)噪声(附Python代码)
如何给图片添加泊松(poisson)噪声(附Python代码)