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

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

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.07824735983398969
PSNR (noisy) = 22.21
PSNR (slow) = 29.30
PSNR (slow, using sigma) = 29.72
PSNR (fast) = 28.88
PSNR (fast, using sigma) = 29.26

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,  # 5x5 patches
    patch_distance=6,  # 13x13 search area
    channel_axis=-1,
)

# 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 分钟 2.921 秒)

由 Sphinx-Gallery 生成的图库