数字图像噪声_Python

一:数字图像噪声来源

图像的获取(数字化过程)和传输过程,当使用相机获取图像时,光照程度和传感器温度是生成图像大量噪点的主要因素。

二:灰度图加入高斯噪声(正态噪声)

def GaussianNoise(src,mean,sigma):
    NoiseImg=src
    rows,cols=NoiseImg.shape
    for i in range(rows):
        for j in range(cols):
            NoiseImg[i,j]=NoiseImg[i,j]+random.gauss(mean,sigma)
            if NoiseImg[i,j]<0:
                NoiseImg[i,j]=0
            elif NoiseImg[i,j]>255:
                NoiseImg[i,j]=255
    return NoiseImg

img=cv2.imread('IMG_4470.JPG',0)
img=GaussianNoise(img,2,4)
cv2.imwrite('IMG_4470_GaussianNoise.jpg',img)
cv2.imshow('IMG_4470_GaussianNoise',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第1张图片原图为:
数字图像噪声_Python_第2张图片

三:彩色图加入高斯噪声

def GaussianNoise_color(src,mean,sigma):
    NoiseImg=src
    rows,cols,_=NoiseImg.shape
    for i in range(rows):
        for j in range(cols):
            NoiseImg[i,j]=NoiseImg[i,j]+random.gauss(mean,sigma)
    return NoiseImg

img=cv2.imread('IMG_4470.JPG')
img=GaussianNoise_color_percentage(img,2,4)
cv2.imwrite('IMG_4470_GaussianNoise_color_1.jpg',img)
cv2.imshow('IMG_4470_GaussianNoise_color_1',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出为:
数字图像噪声_Python_第3张图片

四:部分像素点加入高斯噪声

def GaussianNoise_color_percentage(src,mean,sigma,percentage):
    NoiseImg=src
    rows,cols,_=NoiseImg.shape
    num=int(rows*cols*percentage)
    for i in range(num):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        NoiseImg[randX,randY]=NoiseImg[randX,randY]+random.gauss(mean,sigma)
    return NoiseImg

img=cv2.imread('IMG_4470.JPG')
img=GaussianNoise_color_percentage(img,2,4,0.5)
cv2.imwrite('IMG_4470_GaussianNoise_color_percentage_1.jpg',img)
cv2.imshow('IMG_4470_GaussianNoise_color_percentage_1',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第4张图片

五:图像加入椒盐噪声_胡椒点

def Pepper(src,percentage):
    NoiseImg=src
    rows,cols,_=NoiseImg.shape
    NoiseNum=int(percentage*rows*cols)
    for i in range(NoiseNum):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if random.randint(0,1)<=0.5:
            NoiseImg[randX,randY]=0
        else:
            NoiseImg[randX,randY]=NoiseImg[randX,randY]
    return NoiseImg

img=cv2.imread('IMG_4470.JPG')
img=PepperandSalt_2(img,0.01)
cv2.imwrite('IMG_4470_PepperandSalt_7_pepper.jpg',img)
cv2.imshow('IMG_4470_PepperandSalt_7',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第5张图片

六:图像加入椒盐噪声_盐点

def salt(src,percentage):
    NoiseImg=src
    rows,cols,_=NoiseImg.shape
    NoiseNum=int(percentage*rows*cols)
    for i in range(NoiseNum):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if random.randint(0,1)<=0.5:
            NoiseImg[randX,randY]=255
        else:
            NoiseImg[randX,randY]=NoiseImg[randX,randY]

    return NoiseImg

img=cv2.imread('IMG_4470.JPG')
img=salt(img,0.01)
cv2.imwrite('IMG_4470_PepperandSalt_8_salt.jpg',img)
cv2.imshow('IMG_4470_PepperandSalt_8',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第6张图片

七:灰度图加入椒盐噪声

def PepperandSalt(src,percentage):
    NoiseImg=src
    rows,cols=NoiseImg.shape
    NoiseNum=int(percentage*rows*cols)
    for i in range(NoiseNum):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if random.randint(0,1)<=0.5:
            NoiseImg[randX,randY]=0
        else:
            NoiseImg[randX,randY]=255
    return NoiseImg

img=cv2.imread('IMG_4470.JPG',0)
img=PepperandSalt(img,0.05)
cv2.imwrite('IMG_4470_PepperandSalt_2.jpg',img)
cv2.imshow('IMG_4470_PepperandSalt_2',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第7张图片

八:彩色图加入椒盐噪声

def PepperandSalt_color(src,percentage):
    NoiseImg=src
    rows,cols,_=NoiseImg.shape
    NoiseNum=int(percentage*rows*cols)
    for i in range(NoiseNum):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if random.randint(0,1)<=0.5:
            NoiseImg[randX,randY]=0
        else:
            NoiseImg[randX,randY]=255
    return NoiseImg
    
img=cv2.imread('IMG_4470.JPG')
img=PepperandSalt_color(img,0.05)
cv2.imwrite('IMG_4470_PepperandSalt_color.jpg',img)
cv2.imshow('IMG_4470_PepperandSalt_color',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第8张图片

九:指数分布噪声

def Exponent(src,constant,percentage):
    NoiseImg=src
    rows,cols=NoiseImg.shape
    num=int(rows*cols*percentage)
    for i in range(num):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if NoiseImg[randX,randY]>128:
            NoiseImg[randX,randY]=constant*np.exp(-constant*NoiseImg[randX,randY])          
        else:
            NoiseImg[randX,randY]=NoiseImg[randX,randY]
    return NoiseImg

img=cv2.imread('IMG_4470.JPG',0)
img=Exponent(img,0.5,0.01)
cv2.imwrite('IMG_4470_Exponent.jpg',img)
cv2.imshow('IMG_4470_Exponent',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第9张图片

十:均匀分布噪声

def distribution(src,low,high,percentage):
    NoiseImg=src
    rows,cols=NoiseImg.shape
    num=int(rows*cols*percentage)
    for i in range(num):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if low

输出结果为:
数字图像噪声_Python_第10张图片

十一:伽马(爱尔兰)噪声

伽马曲线如下:

a=10
b=20
z=np.arange(1,20,0.1)
y=(a**b)*(z**(b-1))/np.math.factorial(b-1)*np.exp(-a*z)
plt.plot(z,y)

数字图像噪声_Python_第11张图片

def gamma_noise(src,constant_a,constant_b,percentage):
    NoiseImg=src
    rows,cols=NoiseImg.shape
    num=int(rows*cols*percentage)
    for i in range(num):
        randX=np.random.randint(0,rows-1)
        randY=np.random.randint(0,cols-1)
        if NoiseImg[randX,randY]>120 and b!=0:
            z=NoiseImg[randX,randY]
            NoiseImg[randX,randY]=(a**b)*(z**(b-1))/np.math.factorial(b-1)*np.exp(-a*z)        
        else:
            NoiseImg[randX,randY]=NoiseImg[randX,randY]
    return NoiseImg
    
img=cv2.imread('IMG_4470.JPG',0)
img=gamma_noise(img,5,10,0.01)
cv2.imwrite('IMG_4470_gamma_noise.jpg',img)
cv2.imshow('IMG_4470_gamma_noise',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)

输出结果为:
数字图像噪声_Python_第12张图片

你可能感兴趣的:(图像处理)