From 8de3ec988de42d9ceb92e6d436f7d332eb5d337f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sat, 6 Oct 2012 22:51:16 +0200 Subject: [PATCH] Add example script for bilateral denoising --- doc/examples/plot_denoise.py | 64 ++++++++++++++++++++++++++++ doc/examples/plot_lena_tv_denoise.py | 51 ---------------------- 2 files changed, 64 insertions(+), 51 deletions(-) create mode 100644 doc/examples/plot_denoise.py delete mode 100644 doc/examples/plot_lena_tv_denoise.py diff --git a/doc/examples/plot_denoise.py b/doc/examples/plot_denoise.py new file mode 100644 index 00000000..30dfc10c --- /dev/null +++ b/doc/examples/plot_denoise.py @@ -0,0 +1,64 @@ +""" +============================= +Denoising the picture of Lena +============================= + +In this example, we denoise a noisy version of the picture of Lena using the +total variation and bilateral denoising filter. + +These algorithms typically produce "posterized" images with flat domains +separated by sharp edges. It is possible to change the degree of posterization +by controlling the tradeoff between denoising and faithfulness to the original +image. + +Total variation filter +---------------------- + +The result of this filter is an image that has a minimal total variation norm, +while being as close to the initial image as possible. The total variation is +the L1 norm of the gradient of the image, and minimizing the total variation. + +Bilateral filter +---------------- +A bilateral filter is an edge-preserving and noise reducing denoising filter. +It averages pixel based on their spatial closeness and radiometric similarity. + +""" + +import numpy as np +import matplotlib.pyplot as plt + +from skimage import data, color, img_as_float +from skimage.filter import tv_denoise, denoise_bilateral + +lena = img_as_float(data.lena()) +lena = lena[220:300, 220:320] + +noisy = lena + 0.5 * lena.std() * np.random.random(lena.shape) +noisy = np.clip(noisy, 0, 1) + +fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(8, 5)) + +ax[0, 0].imshow(lena) +ax[0, 0].axis('off') +ax[0, 0].set_title('original') +ax[0, 1].imshow(tv_denoise(noisy, weight=0.02)) +ax[0, 1].axis('off') +ax[0, 1].set_title('TV') +ax[0, 2].imshow(tv_denoise(noisy, weight=0.05)) +ax[0, 2].axis('off') +ax[0, 2].set_title('(more) TV') + +ax[1, 0].imshow(noisy) +ax[1, 0].axis('off') +ax[1, 0].set_title('original') +ax[1, 1].imshow(denoise_bilateral(noisy, sigma_color=0.02, sigma_range=15)) +ax[1, 1].axis('off') +ax[1, 1].set_title('Bilateral') +ax[1, 2].imshow(denoise_bilateral(noisy, sigma_color=0.05, sigma_range=15)) +ax[1, 2].axis('off') +ax[1, 2].set_title('(more) Bilateral') + +fig.subplots_adjust(wspace=0.02, hspace=0.2, top=0.9, bottom=0.05, left=0, right=1) + +plt.show() diff --git a/doc/examples/plot_lena_tv_denoise.py b/doc/examples/plot_lena_tv_denoise.py deleted file mode 100644 index bce07845..00000000 --- a/doc/examples/plot_lena_tv_denoise.py +++ /dev/null @@ -1,51 +0,0 @@ -""" -==================================================== -Denoising the picture of Lena using total variation -==================================================== - -In this example, we denoise a noisy version of the picture of Lena -using the total variation denoising filter. The result of this filter -is an image that has a minimal total variation norm, while being as -close to the initial image as possible. The total variation is the L1 -norm of the gradient of the image, and minimizing the total variation -typically produces "posterized" images with flat domains separated by -sharp edges. - -It is possible to change the degree of posterization by controlling -the tradeoff between denoising and faithfulness to the original image. - -""" - -import numpy as np -import matplotlib.pyplot as plt - -from skimage import data, color, img_as_ubyte -from skimage.filter import tv_denoise - -l = img_as_ubyte(color.rgb2gray(data.lena())) -l = l[230:290, 220:320] - -noisy = l + 0.4 * l.std() * np.random.random(l.shape) - -tv_denoised = tv_denoise(noisy, weight=10) - -plt.figure(figsize=(8, 2)) - -plt.subplot(131) -plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220) -plt.axis('off') -plt.title('noisy', fontsize=20) -plt.subplot(132) -plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220) -plt.axis('off') -plt.title('TV denoising', fontsize=20) - -tv_denoised = tv_denoise(noisy, weight=50) -plt.subplot(133) -plt.imshow(tv_denoised, cmap=plt.cm.gray, vmin=40, vmax=220) -plt.axis('off') -plt.title('(more) TV denoising', fontsize=20) - -plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, bottom=0, left=0, - right=1) -plt.show()