近期写论文的时候要用到给图像加噪声,就在网上百度了一下,发现其中的一篇博客写的很好,就转载过来了,这里我主要转载了博主的第三部分imnoise函数。如果需要更详细的内容,请到原博主博客浏览。希望对大家有所帮助。
博客原文转自 https://blog.csdn.net/liuyingying0418/article/details/79432962
- imnoise()
官方文档说明为:
J = imnoise(I,’gaussian’,M,V) adds Gaussian white noise of mean m and variance v to the image I. The default is zero mean noise with 0.01 variance.
也就是说M表示均值,V表示方差。
那么将添加噪声的代码改为:
distImg = imnoise(origImg, ‘gaussian’, 0, 10的2次方除以255^2);
可得到:
var =
99.3884
std =
9.9694
mean =
-0.0197
Note : The mean and variance parameters for ‘gaussian’, ‘localvar’, and ‘speckle’ noise types are always specified as if the image were of class double in the range [0, 1]. If the input image is of class uint8 or uint16, the imnoise function converts the image to double, adds noise according to the specified type and parameters, and then converts the noisy image back to the same class as the input.
此处解释一下,原始图像origImg是uint8的灰度图像,灰度值范围为[0,255],故imnoise函数在处理时会将图像先转换为[0,1],然后按照所给的均值和方差添加噪声,最后再将图像转换到[0,255]的范围内,因此参数设置尤其要注意,如果所给的图像是uint8时,所给的均值和方差必须是在[0,1]的范围内,也就是说要归一化,不然直接给100的方差时,imnoise函数会将图像归一化,然后按照方差100来添加噪声,这样原先的图像就给淹没了,反而变成了噪声为主导。