import cv2
import os
import numpy as np
import random
def gaussian_noise(img,mean,sigma):
'''
此函数将产生高斯噪声加到图片上
:param img:原图
:param mean:均值
:param sigma:标准差
:return:噪声处理后的图片
'''
img = img/255
noise = np.random.normal(mean, sigma, img.shape)
gaussian_out = img + noise
gaussian_out = np.clip(gaussian_out, 0, 1)
gaussian_out = np.uint8(gaussian_out*255)
return gaussian_out
def convert(input_dir, output_dir):
for filename in os.listdir(input_dir):
path = input_dir + "/" + filename
print("doing... ", path)
noise_img = cv2.imread(path)
img_noise = gaussian_noise(noise_img, 0, 0.12)
cv2.imwrite(output_dir+'/'+filename,img_noise )
if __name__ == '__main__':
input_dir = "C:\\Users\\ljs\\Desktop\\NumberLabels_gaubefor"
output_dir = "C:\\Users\\ljs\\Desktop\\gao_after"
convert(input_dir, output_dir)