第5章 Python 数字图像处理(DIP) – 图像复原与重建3 – 爱尔兰(伽马)噪声

  • Post author:
  • Post category:python




爱尔兰(伽马)噪声

爱尔兰噪声的PDF是





P

(

z

)

=

{

a

b

z

b

1

(

b

1

)

!

e

a

z

,

z

0

0

,

z

<

0

(5.7)

P(z) = \begin{cases} \frac{a^bz^{b-1}}{(b-1)!}e^{-az}, & z\geq 0 \\ 0, & z < 0 \end{cases} \tag{5.7}






P


(


z


)




=










{


























(


b





1


)


!

















a










b










z











b





1






























e














a


z










,








0


,



























z









0








z




<




0



























(



5


.


7



)











a

>

b

a > b






a




>








b





,



b

b






b





是一个正整数,”!”是阶乘

均值和方差为





z

ˉ

=

b

a

(5.8)

\bar{z} = \frac{b}{a} \tag{5.8}














z







ˉ









=



















a














b

























(



5


.


8



)












σ

2

=

b

a

2

(5.9)

\sigma^2 = \frac{b}{a^2} \tag{5.9}







σ










2











=




















a










2





















b

























(



5


.


9



)






仅当分母 为伽马函数



Γ

(

b

)

\Gamma(b)






Γ


(


b


)





时这称为

伽马密度





b

=

1

b=1






b




=








1





时,为指数噪声

def ireland_pdf(z, a=2, b=1):
    """
    create ireland PDF, math $$P(z) = \begin{cases} \frac{a^bz^{b-1}}{(b-1)!}e^{-az}, & z\geq 0 \\ 0, & z < 0 \end{cases}$$
    param: z: input grayscale value of iamge
    param: a: float, a > b
    param: b: uint, 
    """
    ireland = (a**b * z**(b-1) / np.math.factorial(b - 1)) * np.exp(- a *z)
    ireland = np.where(z >= 0, ireland, 0)
    
    return ireland


更正下面代码,如果之前已经复制的,也请更正

def add_gamma_noise(img, scale=1):
    """
    add gamma noise for image
    param: img: input image, dtype=uint8
    param: mean: noise mean
    param: sigma: noise sigma
    return: image_out: image with gamma noise
    """
    # image = np.array(img/255, dtype=float) # 这是有错误的,将得不到正确的结果,修改如下
    image = np.array(img, dtype=float)

    noise = np.random.gamma(shape=1, scale=scale, size=image.shape)
    
    image_out = image + noise
    image_out = np.uint8(normalize(image_out)*255)
    
    return image_out
# 爱尔兰(伽马)噪声
a = 2.1
b = 2
z = np.linspace(0, 10, 200)

z_ = b / a
sigma = b / a**2

print(f"z_ -> {z_}, sigma -> {sigma}")

ireland = ireland_pdf(z, a=a, b=b)

plt.figure(figsize=(9, 6))
plt.plot(z, ireland)
plt.show()
z_ -> 0.9523809523809523, sigma -> 0.4535147392290249

在这里插入图片描述

# 伽马噪声
img_ori = cv2.imread("DIP_Figures/DIP3E_Original_Images_CH05/Fig0503 (original_pattern).tif", 0)
# img_ori = np.ones((512, 512)) * 128
img_gamma = add_gamma_noise(img_ori, scale=20)

plt.figure(figsize=(9, 6))
plt.subplot(121), plt.imshow(img_ori, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img_gamma, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()

在这里插入图片描述

hist, bins = np.histogram(img_gamma.flatten(), bins=255, range=[0, 255], density=True)
bar = plt.bar(bins[:-1], hist[:])

在这里插入图片描述



版权声明:本文为jasneik原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。