Denoising images using Starlet Transform

This example show how to denoise an image using the Starlet transform.

Import required packages

import numpy as np
import matplotlib.pyplot as plt

import pywi
print(pywi.__version__)

from pywi.processing.transform import starlet

Out:

0.3.dev11

Get an image

import pywi.data

img = pywi.data.galaxy()

Plot the original image

fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(img, cmap="gray")
plt.show()
../_images/sphx_glr_plot_starlet_denoising_001.png

Add noise

LAMBDA = 3
MU = 0.
SIGMA = 10.

noised_img = img + np.random.poisson(lam=LAMBDA, size=img.shape) + np.random.normal(loc=MU, scale=SIGMA, size=img.shape)

fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(noised_img, cmap="gray")
plt.show()
../_images/sphx_glr_plot_starlet_denoising_002.png

Compute the wavelet transform

wt_imgs = starlet.wavelet_transform(noised_img, number_of_scales=4)

Plot the wavelet transform coefficients

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(12, 12))

ax1.imshow(wt_imgs[0], cmap="gray")
ax2.imshow(wt_imgs[1], cmap="gray")
ax3.imshow(wt_imgs[2], cmap="gray")
ax4.imshow(wt_imgs[3], cmap="gray")

plt.tight_layout()

plt.show()
../_images/sphx_glr_plot_starlet_denoising_003.png

Plot the wavelet transform coefficients histograms

fig, ax = plt.subplots(figsize=(12, 8))
ax.hist(img.flatten(), bins=50)
plt.show()

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(12, 8))

ax1.hist(wt_imgs[0].flatten(), bins=50)
ax2.hist(wt_imgs[1].flatten(), bins=50)
ax3.hist(wt_imgs[2].flatten(), bins=50)
ax4.hist(wt_imgs[3].flatten(), bins=50)

plt.show()

for i in range(len(wt_imgs)):
    print("scale", i, ":", wt_imgs[i].flatten().sum())
  • ../_images/sphx_glr_plot_starlet_denoising_004.png
  • ../_images/sphx_glr_plot_starlet_denoising_005.png

Out:

scale 0 : -1.3642420526593924e-12
scale 1 : 1.7053025658242404e-12
scale 2 : 3.410605131648481e-12
scale 3 : 7712161.348193087

Filter wavelet coefficients

cleaned_img = pywi.processing.compositing.filter_with_starlet.clean_image(noised_img,
                                                                          filter_thresholds=[100., 0., 0.])

Plot the cleaned image

fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(cleaned_img, cmap="gray")
plt.show()
../_images/sphx_glr_plot_starlet_denoising_006.png

Total running time of the script: ( 0 minutes 4.276 seconds)

Gallery generated by Sphinx-Gallery