保留纹理的非局部均值去噪#

在本例中,我们使用非局部均值滤波器对宇航员图像的细节进行去噪。非局部均值算法用其他一些像素值的平均值替换像素的值:将以其他像素为中心的较小块与以感兴趣像素为中心的块进行比较,并且仅对具有接近当前块的块的像素执行平均。因此,该算法可以很好地恢复纹理,而其他去噪算法会使这些纹理模糊。

fast_mode 参数为 False 时,在计算块距离时,将空间高斯加权应用于块。当 fast_modeTrue 时,将应用一种更快的算法,该算法对块采用均匀的空间加权。

对于这两种情况,如果提供了噪声标准偏差 sigma,则在计算块距离时会减去预期的噪声方差。这可以导致图像质量略有提高。

estimate_sigma 函数可以为设置非局部均值算法的 h(以及可选的 sigma)参数提供一个良好的起点。h 是一个常数,它控制块权重随块之间距离变化的衰减。较大的 h 允许在不同的块之间进行更多平滑。

在本演示中,h 已手动调整以获得每个变体的近似最佳性能。

noisy, non-local means (slow), non-local means (slow, using $\sigma_{est}$), original (noise free), non-local means (fast), non-local means (fast, using $\sigma_{est}$)
estimated noise standard deviation = 0.07868034086869481
PSNR (noisy) = 22.20
PSNR (slow) = 29.44
PSNR (slow, using sigma) = 29.84
PSNR (fast) = 29.05
PSNR (fast, using sigma) = 29.41

import numpy as np
import matplotlib.pyplot as plt

from skimage import data, img_as_float
from skimage.restoration import denoise_nl_means, estimate_sigma
from skimage.metrics import peak_signal_noise_ratio
from skimage.util import random_noise


astro = img_as_float(data.astronaut())
astro = astro[30:180, 150:300]

sigma = 0.08
noisy = random_noise(astro, var=sigma**2)

# estimate the noise standard deviation from the noisy image
sigma_est = np.mean(estimate_sigma(noisy, channel_axis=-1))
print(f'estimated noise standard deviation = {sigma_est}')

patch_kw = dict(
    patch_size=5, patch_distance=6, channel_axis=-1  # 5x5 patches  # 13x13 search area
)

# slow algorithm
denoise = denoise_nl_means(noisy, h=1.15 * sigma_est, fast_mode=False, **patch_kw)

# slow algorithm, sigma provided
denoise2 = denoise_nl_means(
    noisy, h=0.8 * sigma_est, sigma=sigma_est, fast_mode=False, **patch_kw
)

# fast algorithm
denoise_fast = denoise_nl_means(noisy, h=0.8 * sigma_est, fast_mode=True, **patch_kw)

# fast algorithm, sigma provided
denoise2_fast = denoise_nl_means(
    noisy, h=0.6 * sigma_est, sigma=sigma_est, fast_mode=True, **patch_kw
)

fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(8, 6), sharex=True, sharey=True)

ax[0, 0].imshow(noisy)
ax[0, 0].axis('off')
ax[0, 0].set_title('noisy')
ax[0, 1].imshow(denoise)
ax[0, 1].axis('off')
ax[0, 1].set_title('non-local means\n(slow)')
ax[0, 2].imshow(denoise2)
ax[0, 2].axis('off')
ax[0, 2].set_title('non-local means\n(slow, using $\\sigma_{est}$)')
ax[1, 0].imshow(astro)
ax[1, 0].axis('off')
ax[1, 0].set_title('original\n(noise free)')
ax[1, 1].imshow(denoise_fast)
ax[1, 1].axis('off')
ax[1, 1].set_title('non-local means\n(fast)')
ax[1, 2].imshow(denoise2_fast)
ax[1, 2].axis('off')
ax[1, 2].set_title('non-local means\n(fast, using $\\sigma_{est}$)')

fig.tight_layout()

# print PSNR metric for each case
psnr_noisy = peak_signal_noise_ratio(astro, noisy)
psnr = peak_signal_noise_ratio(astro, denoise)
psnr2 = peak_signal_noise_ratio(astro, denoise2)
psnr_fast = peak_signal_noise_ratio(astro, denoise_fast)
psnr2_fast = peak_signal_noise_ratio(astro, denoise2_fast)

print(f'PSNR (noisy) = {psnr_noisy:0.2f}')
print(f'PSNR (slow) = {psnr:0.2f}')
print(f'PSNR (slow, using sigma) = {psnr2:0.2f}')
print(f'PSNR (fast) = {psnr_fast:0.2f}')
print(f'PSNR (fast, using sigma) = {psnr2_fast:0.2f}')

plt.show()

脚本的总运行时间:(0 分钟 1.949 秒)

由 Sphinx-Gallery 生成的图库