Python OpenCV彩色圖片新增隨機椒鹽噪聲
阿新 • • 發佈:2020-12-15
def sp_noiseImg(img_file1,prob): #同時加雜亂(RGB單噪聲)RGB圖噪聲 prob:噪聲佔比 image = np.array(img_file1,dtype=float) height = image.shape[0] width = image.shape[1] channels = image.shape[2] #prob = 0.05 #噪聲佔比 已經比較明顯了 >0.1 嚴重影響畫質 NoiseImg = image.copy() NoiseNum = int(prob * image.shape[0] * image.shape[1]) print("椒鹽噪聲圖") print("噪聲數量=",NoiseNum) for i in range(NoiseNum): rows = np.random.randint(0, image.shape[0] - 1) cols = np.random.randint(0, image.shape[1] - 1) channel = np.random.randint(0, 3) if np.random.randint(0, 2) == 0:#隨機加鹽或者加椒 NoiseImg[rows, cols, channel] = 0 else: NoiseImg[rows, cols, channel] = 255 return NoiseImg ```