python添加噪声

Python-图像加噪

高斯噪声

高斯噪声(Gaussian noise)是指它的概率密度函数服从高斯分布的一类噪声。如果一个噪声,它的幅度分布服从高斯分布,而它的功率谱密度又是均匀分布的,则称它为高斯白噪声。 
注意:“高斯白噪声的幅度服从高斯分布”的说法是错误的,高斯噪声的幅度服从瑞利分布。即使是一维的高斯白噪声,其幅度也不会服从高斯分布,而应该服从瑞利分布。二维不相关的复高斯白噪声包络服从指数分布(x2分布的自由度为2的特例)。n个不相关的复高斯白噪声序列叠加后的复信号包络服从自由度为2n的x2分 布。 
必须区分高斯噪声和白噪声两个不同的概念。高斯噪声是指噪声的概率密度函数服从高斯分布,白噪声是指噪声的任意两个采样样本之间不相关,两者描述的角度不同。白噪声不必服从高斯分布,高斯分布的噪声不一定是白噪声。当然,实际系统中的热噪声是我们一般所说的白噪声的主要来源,它是服从高斯分布的,但一般具有有限的带宽,即常说的窄带白噪声,严格意义上它不是白噪声。

椒盐噪声

椒盐噪声(salt-and-pepper noise)是指两种噪声,一种是盐噪声(salt noise),另一种是胡椒噪声(pepper noise)。盐=白色(0),椒=黑色(255)。前者是高灰度噪声,后者属于低灰度噪声。一般两种噪声同时出现,呈现在图像上就是黑白杂点。

添加噪声的实现

以上两种噪声,均可通过Python库:skimage实现。

安装skimage库

必须的依赖:

//  python-numpy  
//  python-scipy  
//  python-matplotlib  
$  sudo apt-get install python-numpy  
$  sudo apt-get install python-scipy  
$  sudo apt-get install python-matplotlib  
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

安装python-scikit-image

$  sudo apt-get install python-skimage 
 
 
  • 1
相关函数介绍

skimage.util.random_noise(imagemode=’gaussian’seed=Noneclip=True**kwargs)

参数说明

image : ndarray

Input image data. Will be converted to float.

mode : str

One of the following strings, selecting the type of noise to add:

  • ‘gaussian’ Gaussian-distributed additive noise.

  • ‘localvar’ Gaussian-distributed additive noise, with specified local variance at each point of image

  • ‘poisson’ Poisson-distributed noise generated from the data.

  • ‘salt’ Replaces random pixels with 1.

  • ‘pepper’ Replaces random pixels with 0 (for unsigned images) or -1 (for signed images).

  • ‘s&p’ Replaces random pixels with either 1 or low_val, where low_val is 0 for unsigned images or -1 for signedimages.

  • ‘speckle’ Multiplicative noise using out = image + n*image, where n is uniform noise with specified mean & variance.

seed : int

If provided, this will set the random seed before generating noise,for valid pseudo-random comparisons.

clip : bool

If True (default), the output will be clipped after noise appliedfor modes ‘speckle’, ‘poisson’, and ‘gaussian’. This isneeded to maintain the proper image data range. If False, clippingis not applied, and the output may extend beyond the range [-1, 1].

mean : float

Mean of random distribution. Used in ‘gaussian’ and ‘speckle’.Default : 0.

var : float

Variance of random distribution. Used in ‘gaussian’ and ‘speckle’.Note: variance = (standard deviation) ** 2. Default : 0.01

local_vars : ndarray

Array of positive floats, same shape as image, defining the localvariance at every image point. Used in ‘localvar’.

amount : float

Proportion of image pixels to replace with noise on range [0, 1].Used in ‘salt’, ‘pepper’, and ‘salt & pepper’. Default : 0.05

salt_vs_pepper : float

Proportion of salt vs. pepper noise for ‘s&p’ on range [0, 1].Higher values represent more salt. Default : 0.5 (equal amounts)

Output

out : ndarray

Output floating-point image data on range [0, 1] or [-1, 1] if theinput image was unsigned or signed, respectively.

注意事项
  1. Peckle, Poisson, Localvar, and Gaussian noise 加上噪声后,值可能为负值,也可能超过255;默认情况下,clip参数值为True,将会clip掉这些超过区间的点,如果clip设置为False,就要注意有可能包含一些超过区间的点。
  2. Skimage读取图像是RGB,而Opencv是BGR
  3. Skimage读取图像后是(height, width, channel)
图示

OriginImg:

OriginImg

OriginImg + Add gaussian noise:

gauss_noise_img

OriginImg + Add salt noise:

sale_noise_img

OriginImg + Add pepper noise:

pepper_noise_img

OriginImg + Add salt and pepper noise: 
salt_pepper_img

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值