0
点赞
收藏
分享

微信扫一扫

【OpenCV 完整例程】92. 指数噪声、均匀噪声、椒盐噪声

【OpenCV 完整例程】92. 指数噪声、均匀噪声、椒盐噪声


2. 噪声模型

数字图像中的噪声源主要来自图像获取和传输过程。在获取图像时,光照水平和传感器温度影响图像中的噪声。在传输图像时,传输信道中的干扰对图像产生污染。


2.4 指数噪声 (Exponential Noise)

指数噪声的概率密度函数为
p ( z ) = { a e − a z , z ≥ 0 0 , z < 0 p(z) = \begin{cases} ae^{-az} &, z \ge 0\\ 0&, z < 0 \end{cases} p(z)={aeaz0,z0,z<0

指数噪声的均值和标准差为:
z ˉ = 1 / a σ 2 = 1 / a 2 \bar{z} = 1/a \\ \sigma ^2 = 1/a^2 zˉ=1/aσ2=1/a2

显然,指数噪声是爱尔兰噪声在 b=1 时的特殊情况。


例程 9.4:指数噪声 (Exponent Noise)

    # # 9.4:指数噪声 (Exponential noise)
    img = cv2.imread("../images/Fig0503.tif", 0)  # flags=0 读取为灰度图像
    # img = np.ones([256, 256]) * 128

    a = 10.0
    noiseExponent = np.random.exponential(scale=a, size=img.shape)
    imgExponentNoise = img + noiseExponent
    imgExponentNoise = np.uint8(cv2.normalize(imgExponentNoise, None, 0, 255, cv2.NORM_MINMAX))  # 归一化为 [0,255]

    plt.figure(figsize=(9, 3))
    plt.subplot(131), plt.title("Origin"), plt.axis('off')
    plt.imshow(img, 'gray', vmin=0, vmax=255)
    plt.subplot(132), plt.title("Exponential noise"), plt.axis('off')
    plt.imshow(imgExponentNoise, 'gray')
    plt.subplot(133), plt.title("Gray hist")
    histNP, bins = np.histogram(imgExponentNoise.flatten(), bins=255, range=[0, 255], density=True)
    plt.bar(bins[:-1], histNP[:])
    plt.tight_layout()
    plt.show()

在这里插入图片描述


2.5 均匀噪声 (Uniform Noise)

均匀噪声的概率密度函数为
p ( z ) = { 1 / ( b − a ) , a ≤ z ≤ b 0 , o t h e r p(z) = \begin{cases} 1/(b-a) &, a \le z \le b\\ 0&, other \end{cases} p(z)={1/(ba)0,azb,other

均匀噪声的均值和标准差为:
z ˉ = ( a + b ) / 2 σ 2 = ( b − a ) 2 / 12 \bar{z} = (a+b)/2 \\ \sigma ^2 = (b-a)^2/12 zˉ=(a+b)/2σ2=(ba)2/12


例程 9.5:均匀噪声 (Uniform Noise)

    # # 9.5:均匀噪声 (Uniform noise)
    img = cv2.imread("../images/Fig0503.tif", 0)  # flags=0 读取为灰度图像
    # img = np.ones([256, 256]) * 128

    mean, sigma = 10, 100
    a = 2 * mean - np.sqrt(12 * sigma)  # a = -14.64
    b = 2 * mean + np.sqrt(12 * sigma)  # b = 54.64
    noiseUniform = np.random.uniform(a, b, img.shape)
    imgUniformNoise = img + noiseUniform
    imgUniformNoise = np.uint8(cv2.normalize(imgUniformNoise, None, 0, 255, cv2.NORM_MINMAX))  # 归一化为 [0,255]

    plt.figure(figsize=(9, 3))
    plt.subplot(131), plt.title("Origin"), plt.axis('off')
    plt.imshow(img, 'gray', vmin=0, vmax=255)
    plt.subplot(132), plt.title("Uniform noise"), plt.axis('off')
    plt.imshow(imgUniformNoise, 'gray')
    plt.subplot(133), plt.title("Gray hist")
    histNP, bins = np.histogram(imgUniformNoise.flatten(), bins=255, range=[0, 255], density=True)
    plt.bar(bins[:-1], histNP[:])
    plt.tight_layout()
    plt.show()

在这里插入图片描述


2.6 椒盐噪声 (Salt-pepper Noise)

椒盐噪声的概率密度函数为
p ( z ) = { P s , z = 2 k − 1 P p , z = 0 1 − ( P s + P p ) , z = V p(z) = \begin{cases} Ps &, z = 2^k -1 \\ Pp &, z = 0 \\ 1-(Ps+Pp)&, z = V \end{cases} p(z)=PsPp1(Ps+Pp),z=2k1,z=0,z=V

当 Ps、Pp 都不为 0 时,噪声值是白色的 ( 2 k − 1 ) (2^k-1) (2k1) 或黑色的 ( 0 ) (0) (0),就像盐粒或胡椒粒那样随机地分布在整个图像上,因此称为椒盐噪声,也称为双极冲击噪声。当 Ps 或 Pp 为 0 时,称为单极冲击噪声。

椒盐噪声的均值和标准差为:
z ˉ = ( 0 ) P p + K ( 1 − P s − P p ) + ( 2 k − 1 ) P s σ 2 = ( 0 − z ˉ ) 2 P p + ( K − z ˉ ) 2 ( 1 − P s − P p ) + ( 2 k − 1 ) 2 P s \bar{z} = (0)Pp+K(1-Ps-Pp)+(2^k-1)Ps \\ \sigma ^2 = (0-\bar{z})^2 Pp+(K-\bar{z})^2(1-Ps-Pp)+(2^k-1)^2Ps zˉ=(0)Pp+K(1PsPp)+(2k1)Psσ2=(0zˉ)2Pp+(Kzˉ)2(1PsPp)+(2k1)2Ps

像素被白色盐粒、黑色胡椒粒污染的概率 P 称为噪声密度:
P = P s + P p P = Ps + Pp P=Ps+Pp
例如,Ps=0.05,Pp=0.02,则噪声密度 P=0.07,表示图像中约 5% 的像素被盐粒噪声污染,约 2% 的像素被胡椒粒噪声污染,噪声密度为 7%,即图像中 7% 的像素被椒盐噪声污染。


例程 9.6:椒盐噪声 (Salt-pepper Noise)

    # # 9.6:椒盐噪声 (Salt-pepper)
    img = cv2.imread("../images/Fig0503.tif", 0)  # flags=0 读取为灰度图像

    ps, pp = 0.05, 0.02
    mask = np.random.choice((0, 0.5, 1), size=img.shape[:2], p=[pp, (1-ps-pp), ps])
    imgChoiceNoise = img.copy()
    imgChoiceNoise[mask==1] = 255
    imgChoiceNoise[mask==0] = 0

    plt.figure(figsize=(9, 3))
    plt.subplot(131), plt.title("Origin"), plt.axis('off')
    plt.imshow(img, 'gray', vmin=0, vmax=255)
    plt.subplot(132), plt.title("Choice noise"), plt.axis('off')
    plt.imshow(imgChoiceNoise, 'gray')
    plt.subplot(133), plt.title("Gray hist")
    histNP, bins = np.histogram(imgChoiceNoise.flatten(), bins=255, range=[0, 255], density=True)
    plt.bar(bins[:-1], histNP[:])
    plt.tight_layout()
    plt.show()

在这里插入图片描述


(本节完)


版权声明:

youcans@xupt 原创作品,转载必须标注原文链接

Copyright 2021 youcans, XUPT

Crated:2022-2-1


举报

相关推荐

0 条评论