1. 程式人生 > >給影象加噪聲(椒鹽噪聲)

給影象加噪聲(椒鹽噪聲)

原理:隨機的將影象某些畫素值改為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)