第5章 Python 数字图像处理(DIP) - 图像复原与重建5 - 均匀噪声

标题

均匀噪声

均匀噪声的PDF为
P ( z ) = { 1 b − a , a ≤ z ≤ b 0 , other (5.13) P(z) = \begin{cases}\frac{1}{b-a}, & a\leq z \leq b \\ 0, & \text{other}\end{cases} \tag{5.13} P(z)={ba1,0,azbother(5.13)

均值和方差为
z ˉ = a + b 2 (5.14) \bar{z} = \frac{a + b}{2} \tag{5.14} zˉ=2a+b(5.14)
σ 2 = ( b − a ) 2 12 (5.15) \sigma^2 = \frac{(b - a)^2}{12} \tag{5.15} σ2=12(ba)2(5.15)

def average_pdf(z, a=1, b=2):
    """
    create average PDF, math $$P(z) = \begin{cases}\frac{1}{b-a}, & a\leq z \leq b \\ 0, & \text{other}\end{cases} $$
    param: z: input grayscale value of iamge
    param: a: uint, if image value. it could be float as well.
    param: b: uint, if image value. b > a, it could be float as well.
    """
    assert b > a, "b must greater than a"
    
    average = 1 / (b - a)
    average = np.where(z < a, 0, average)
    average = np.where(z > b, 0, average)
    
    return average

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

def add_average_noise(img, mean=0, sigma=800):
    """
    add average noise for image
    param:  img:     input image, dtype=uint8
    param:  mean:     noise mean
    param:  sigma:     noise sigma
    return: image_out: image with average noise
    """
    # image = np.array(img/255, dtype=float) # 这是有错误的,将得不到正确的结果,修改如下
    image = np.array(img, dtype=float)
    
    a = 2 * mean - np.sqrt(12 * sigma)
    b = 2 * mean + np.sqrt(12 * sigma)
    
    noise = np.random.uniform(a, b, image.shape)
    image_out = image + noise
    
    image_out = np.uint8(normalize(image_out)*255)
    
    return image_out    
# 均匀噪声
a = 8
b = 9
z = np.linspace(0, 10, 200)

z_ = (a + b) / 2
sigma = (b - a)**2 / 12

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

mean = average_pdf(z, a=a, b=b)

plt.figure(figsize=(9, 6))
plt.plot(z, mean)
plt.show()
z_ -> 8.5, sigma -> 0.08333333333333333

在这里插入图片描述

# 均匀噪声
# img_ori = np.ones((512, 512)) * 128
img_ori = cv2.imread("DIP_Figures/DIP3E_Original_Images_CH05/Fig0503 (original_pattern).tif", 0)
img_average = add_average_noise(img_ori, mean=10, sigma=150)

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_average, 'gray', vmin=0, vmax=255), plt.xticks([]), plt.yticks([])

plt.tight_layout()
plt.show()

在这里插入图片描述

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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jasneik

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值