python 对图片添加高斯噪声和椒盐噪声代码,并采用空间域滤波器进行过滤

课程实验1图像滤波

  1. 请叙述常见的噪声来源与特性,包括加性噪声、乘性噪声、高斯噪声和椒盐噪声。

(1)加性噪声一般指热噪声、散弹噪声等,它们与信号的关系是相加,不管有没有信号,噪声都存在。信道中加性噪声的来源,一般可以分为三方面:

1.人为噪声:人为噪声来源于无关的其它信号源,例如:外台信号、开关接触噪声、工业的点火辐射等。

2.自然噪声:自然噪声是指自然界存在的各种电磁波源,例如:闪电、雷击、大气中的电暴和各种宇宙噪声等。

3.内部噪声: 内部噪声是系统设备本身产生的各种噪声,例如:电阻中自由电子的热运动和半导体中载流子的起伏变化等。

(2)乘性噪声是信道特性随机变化引起的噪声,它主要表现在无线电通信传输信道中,例如,电离层和对流层的随机变化引起信号不反应任何消息含义的随机变化,而构成对信号的干扰。这类噪声只有在信号出现在上述信道中才表现出来,它不会主动对信号形成干扰,因此称之为乘性噪声。

(3)高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。如果一个噪声,它的幅度分布服从高斯分布,而它的功率谱密度又是均匀分布的,则称它为高斯白噪声。高斯白噪声的二阶矩不相关,一阶矩为常数,是指先后信号在时间上的相关性。产生原因:

1.图像传感器在拍摄时市场不够明亮、亮度不够均匀;

2.电路各元器件自身噪声和相互影响;

3.图像传感器长期工作,温度过高。

(4)椒盐噪声,椒盐噪声是数字图像的一个常见噪声,又称脉冲噪声,所谓椒盐,椒就是黑,盐就是白,椒盐噪声就是在图像上随机出现黑色白色的像素。椒盐噪声是一种因为信号脉冲强度引起的噪声,它随机改变一些像素值。是由图像传感器, 传输信道,解码处理等产生的黑白相间的亮暗点噪声。椒盐噪声往往由图像切割引起。椒盐噪声产生的原因:环境中的干扰(如电磁干扰),传感器(ADC)内部时序错误等。

 

  1. 分别对图片添加高斯噪声和椒盐噪声,并采用空间域滤波器进行过滤,包括均值滤波器,高斯滤波器和中值滤波器。

1)对图片添加高斯噪声和椒盐噪声:

原图:

代码:

import cv2

import os

import numpy as np

 

def Expand2Dto3D(img):

    img = np.expand_dims(img, axis=2)

    img = np.repeat(img, 3, axis=2)

    return img

 

def AddSaltAndPepperNosie(img, pro):

    noise = np.random.uniform(0, 255, img[:, :, 0].shape)

    mask = noise < pro * 255

    mask = Expand2Dto3D(mask)

    img = img * (1 - mask)

 

    mask = noise > 255 - pro * 255

    mask = Expand2Dto3D(mask)

    img = 255 * mask + img * (1 - mask)

    return img

 

def AddGaussNoise(img, sigma, mean=0):

    # 大概率abs(noise) < 3 * sigma

    noise = np.random.normal(mean, sigma, img.shape)

    img = img.astype(np.float64)

    img = img + noise

    img = np.clip(img, 0, 255)

    img = img.astype(np.uint8)

    return img

 

 

if __name__ == '__main__':

    img = cv2.imread('D:/tz/fingerprint.jpg', 1)

    print(img.shape)

    noiseImg = AddGaussNoise(img, 20, 0)

    cv2.imwrite('D:/tz/Guassion_noise.jpg', noiseImg)

 

    noiseImgSalt = AddSaltAndPepperNosie(img, 0.1)

    cv2.imwrite('D:/tz/Peppers_spnoise.jpg', noiseImgSalt)

 

添加高斯噪声与椒盐噪声结果:

 

 

 

2)采用空间域滤波器进行过滤

2.1均值滤波器,也称低通滤波器,即对滤波核内的数据求均值,然后将这个值赋值给矩阵核心位置。均值滤波器可以使用cv2.blur() 方法实现。

代码:

import cv2

img = cv2.imread("D:/tz/Peppers_spnoise.jpg")

dst2 = cv2.blur(img, (5, 5))

cv2.imshow("5*5", dst2)

cv2.waitKey()

cv2.destroyAllWindows()

 

均值滤波后结果:

             

2.2高斯平滑滤波是一种应用较广泛的平滑空间滤波方法之一,高斯滤波器也被称为高斯模糊或高斯平滑。高斯滤波器可以在降低图片噪声、细节层次的同时保留更多的图像信息,使经过处理的图像呈现出“磨砂玻璃”的滤镜效果。使用高斯滤波器求的是不同权重下的均值,越靠近核心的像素的权重越大,约靠近边缘的像素的权重则越小。

代码:

import cv2

img = cv2.imread("D:/tz/Peppers_spnoise.jpg")

dst1 = cv2.GaussianBlur(img, (9, 9), 0, 0)

cv2.imshow("9*9", dst1)

cv2.waitKey()

cv2.destroyAllWindows()

高斯滤波结果:

 

      

2.3中值滤波器,即对滤波核内所有数据排序,将中间值赋值给滤波核核心位置的数字

代码:

import cv2

img = cv2.imread("D:/tz/Peppers_spnoise.jpg")

dst1 = cv2.medianBlur(img, 5)

cv2.imshow("5*5", dst1)

cv2.waitKey()

cv2.destroyAllWindows()

中值滤波结果:

 

   

  1. 采用均方误差MSE对上述2中的结果进行量化评估,并总结不同噪声适用滤波器情形。

 

注:MSE是估计值与真实值之差的平方的均值,其计算方法为

 

均方误差越小,说明两幅图像之间的相似度越高

 

求均方误差代码:

#import skimage.measure

from skimage.metrics import structural_similarity as ssim

# from skimage import measure

import matplotlib.pyplot as plt

import numpy as np

import cv2

 

 

def mse(imageA, imageB):

    # the 'Mean Squared Error' between the two images is the

    # sum of the squared difference between the two images;

    # NOTE: the two images must have the same dimension

    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)

    err /= float(imageA.shape[0] * imageA.shape[1])

 

    # return the MSE, the lower the error, the more "similar"

    # the two images are

    return err

 

def compare_images(imageA, imageB, title):

    # compute the mean squared error and structural similarity

    # index for the images

    m = mse(imageA, imageB)

    s = ssim(imageA, imageB)

    # setup the figure

    fig = plt.figure(title)

    plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))

    # show first image

    ax = fig.add_subplot(1, 2, 1)

    plt.imshow(imageA, cmap=plt.cm.gray)

    plt.axis("off")

    # show the second image

    ax = fig.add_subplot(1, 2, 2)

    plt.imshow(imageB, cmap=plt.cm.gray)

    plt.axis("off")

    # show the images

    plt.show()

 

# load the images -- the original, the original + contrast,

# and the original + photoshop

original = cv2.imread("D:/tz/fingerprint.jpg")

contrast = cv2.imread("D:/tz/Guassion_gs_jz.png")

shopped = cv2.imread("D:/tz/Peppers_gs_jz.png")

# convert the images to grayscale

original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)

contrast = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)

shopped = cv2.cvtColor(shopped, cv2.COLOR_BGR2GRAY)

 

# initialize the figure

fig = plt.figure("Images")

images = ("Original", original), ("Contrast", contrast), ("Photoshopped", shopped)

# loop over the images

for (i, (name, image)) in enumerate(images):

    # show the image

    ax = fig.add_subplot(1, 3, i + 1)

    ax.set_title(name)

    plt.imshow(image, cmap = plt.cm.gray)

    plt.axis("off")

# show the figure

plt.show()

# compare the images

compare_images(original, original, "Original vs. Original")

compare_images(original, contrast, "Original vs. Contrast")

compare_images(original, shopped, "Original vs. Photoshopped")

 

1、 中值滤波法是一种非线性平滑技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值.

2、 均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临近像素(以目标像素为中心的周围8个像素,构成一个滤波模板,即去掉目标像素本身),再用模板中的全体像素的平均值来代替原来像素值。

3、 高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。

4、 图像经过中值滤波后,高斯噪声没有被完全去除,椒盐噪声几乎被完全去除效果较好。经过均值滤波后不管是高斯噪声还是椒盐噪声大部分都没有被去除,只是稍微模糊化。经过高斯滤波后,高斯噪声和椒盐噪声几乎被很大程度的模糊化,原图好像被加上了一层蒙版。

posted @ 2022-05-09 16:39  yinghualeihenmei  阅读(1145)  评论(0编辑  收藏  举报