## 图像噪声之高斯噪声（gauss noise）

### 算法步骤：

   Pout = Pin + F(means,sigma)


高斯分布
random.gauss(mu, sigma)
Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function defined below.

• a.设定參数sigma 和 Xmean

• b.产生一个高斯随机数

• c.依据输入像素计算出输出像素

• d.又一次将像素值限制或放缩在[0 ~ 255]之间

• e.循环全部像素

• f.输出图像。

### 编程实例：

# -*- coding: utf-8 -*-

from PIL import Image

from pylab import *

from numpy import*

import random

#读取图片并转为数组
im = array(Image.open('./source/test.jpg'))

#设定高斯函数的偏移
means = 0

#设定高斯函数的标准差
sigma = 25

#r通道
r = im[:,:,0].flatten()

#g通道
g = im[:,:,1].flatten()

#b通道
b = im[:,:,2].flatten()

#计算新的像素值
for i in range(im.shape[0]*im.shape[1]):

pr = int(r[i]) + random.gauss(0,sigma)

pg = int(g[i]) + random.gauss(0,sigma)

pb = int(b[i]) + random.gauss(0,sigma)

if(pr < 0):

pr = 0

if(pr > 255):

pr = 255

if(pg < 0):

pg = 0

if(pg > 255):

pg = 255

if(pb < 0):

pb = 0

if(pb > 255):

pb = 255

r[i] = pr

g[i] = pg

b[i] = pb

im[:,:,0] = r.reshape([im.shape[0],im.shape[1]])

im[:,:,1] = g.reshape([im.shape[0],im.shape[1]])

im[:,:,2] = b.reshape([im.shape[0],im.shape[1]])

#显示图像

imshow(im)

show()



# -*- coding: utf-8 -*-

from PIL import Image

from pylab import *

from numpy import*

import random

#读取图片并转为数组
im = array(Image.open('./source/test.jpg'))

#设定高斯函数的偏移
means = 0

#设定高斯函数的标准差
sigma = 25

#r通道
r = im[:,:,0].flatten()

#g通道
g = im[:,:,1].flatten()

#b通道
b = im[:,:,2].flatten()

#计算新的像素值
for i in range(im.shape[0]*im.shape[1]):

r[i] = r[i] + random.gauss(0,sigma)

g[i] = g[i] + random.gauss(0,sigma)

b[i] = b[i] + random.gauss(0,sigma)

im[:,:,0] = r.reshape([im.shape[0],im.shape[1]])

im[:,:,1] = g.reshape([im.shape[0],im.shape[1]])

im[:,:,2] = b.reshape([im.shape[0],im.shape[1]])

#显示图像

imshow(im)

show()


sigma = 15

sigma = 25

sigma = 40