Python+opencv3对图像添加高斯噪声和椒盐噪声

1) 对图像做灰度变换
2) 向图像添加10%的高斯噪声
3) 向图像添加10%的椒盐噪声

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

from numpy import *
from scipy import *
import numpy as np
import cv2
#定义添加椒盐噪声的函数
def SaltAndPepper(src,percetage):
SP_NoiseImg=src
SP_NoiseNum=int(percetage*src.shape[0]*src.shape[1])
for i in range(SP_NoiseNum):
randX=random.random_integers(0,src.shape[0]-1)
randY=random.random_integers(0,src.shape[1]-1)
if random.random_integers(0,1)==0:
SP_NoiseImg[randX,randY]=0
else:
SP_NoiseImg[randX,randY]=255
return SP_NoiseImg
#定义添加高斯噪声的函数
G_Noiseimg = image
G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
for i in range(G_NoiseNum):
temp_x = np.random.randint(20,40)
temp_y = np.random.randint(20,40)
G_Noiseimg[temp_x][temp_y] = 255
return G_Noiseimg
if __name__ == "__main__":
cv2.namedWindow("Original image")
cv2.imshow("Original image", srcImage)
grayImage = cv2.cvtColor(srcImage,cv2.COLOR_BGR2GRAY) #灰度变换
cv2.imshow("grayimage", grayImage)

cv2.imwrite("Glena.jpg ",gauss_noiseImage)

SaltAndPepper_noiseImage = SaltAndPepper(grayImage,0.1) #再添加10%的椒盐噪声

cv2.waitKey(0)
cv2.destroyAllWindows()

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120