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