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,高斯噪声