给图像加噪声(椒盐噪声)

原理:随机的将图像某些像素值改为0或255

添加椒盐噪声

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
img=cv2.imread(r"C:\Users\Jimmy\Desktop\ceshi1.jpg")
cv2.imshow("input",img)
percetage=0.1
SaltAndPepper(img,percetage)
cv2.imshow("salt",img)
cv2.waitKey(0)

 

posted @ 2018-11-21 16:04  小小小小小码农  阅读(3041)  评论(0编辑  收藏  举报