import skimage import os from skimage import io import random def addPoissonNoise(s): var = random.uniform(0.01, 0.2) origin = skimage.io.imread(s) noisy = skimage.util.random_noise(origin, mode='poisson') return noisy * 255 def addGaussNoise(s): var = random.uniform(0.0001, 0.04) origin = skimage.io.imread(s) noisy = skimage.util.random_noise(origin, mode='gaussian',var=var) return noisy*255 def addSaltNoise(s): var = random.uniform(0.01, 0.2) origin = skimage.io.imread(s) noisy = skimage.util.random_noise(origin, mode='s&p', amount=var) return noisy*255 def addSpeckleNoise(s): var = random.uniform(0.0001, 0.04) origin = skimage.io.imread(s) noisy = skimage.util.random_noise(origin, mode='speckle', var=var) return noisy*255
函数定义如上,使用方法如下,具体参数见random_noise函数的源码
if noise == 0: # 如果是0,什么都不做 print("no noise") elif noise == 1: # 添加高斯噪声 image = addGaussNoise(image_path) image = Image.fromarray(np.uint8(image)) elif noise == 2: # 添加poission噪声 image = addPoissonNoise(image_path) image = Image.fromarray(np.uint8(image)) elif noise == 3: # 添加乘法噪声 image = addGaussNoise(image_path) image = Image.fromarray(np.uint8(image))
标签:origin,noise,image,random,噪声,添加,深度,var,skimage From: https://www.cnblogs.com/dark-blue/p/16863217.html