Deblurring with multiple wavelet priorsΒΆ

This tutorial considers the problem of deblurring an image that has been convolved with a 2D Gaussian kernel and contaminated by random noise:

\[\mathbf{y} = \mathbf{B} \mathbf{x} + \mathbf{n}\]

where \(\mathbf{B}\) is a 2D convolution operator and \(\mathbf{n} \sim \mathcal{N}(0, \sigma^2)\).

Natural images are rarely well represented by a single sparsifying transform: piecewise-constant regions with sharp edges are compactly described by the Haar wavelet, whilst smoother, textured regions are better captured by higher-order wavelets such as the Daubechies-4 (db4). We will therefore promote sparsity in both domains at the same time by solving:

\[\argmin_\mathbf{x} \frac{1}{2}\|\mathbf{B}\mathbf{x}-\mathbf{y}\|_2^2 + \lambda_1 \|\mathbf{W}_1 \mathbf{x}\|_1 + \lambda_2 \|\mathbf{W}_2 \mathbf{x}\|_1\]

where \(\mathbf{W}_1\) and \(\mathbf{W}_2\) are the Haar and db4 2D wavelet transforms, respectively.

By stacking the two wavelet transforms into a single operator \(\mathbf{K} = [\mathbf{W}_1^T, \mathbf{W}_2^T]^T\), the regularization term becomes a separable function of \(\mathbf{K}\mathbf{x}\), whose proximal operator is simply the stack of the proximal operators of the two L1 norms. This problem can therefore be solved with the pyproximal.optimization.primaldual.PrimalDual solver (Chambolle-Pock algorithm).

import matplotlib.pyplot as plt
import numpy as np
import pylops
from pylops.utils.metrics import psnr

import pyproximal

plt.close("all")
np.random.seed(0)

Let’s start by loading an image, converting it to grayscale, and cropping it to a square of size power of two (so that the wavelet transforms do not require any padding).

im = plt.imread("../testdata/butterfly.png")
im = np.mean(im[..., :3], axis=-1)

n = 256
h, w = im.shape
im = im[(h - n) // 2 : (h - n) // 2 + n, (w - n) // 2 : (w - n) // 2 + n]
ny, nx = im.shape

We now create a Gaussian blurring kernel and the associated pylops.signalprocessing.Convolve2D operator. This is used to create the blurred data, to which we add white Gaussian noise with a signal-to-noise ratio of 30 dB.

nh = 11
sigh = 2.0
t = np.arange(-(nh // 2), nh // 2 + 1)
hy, hx = np.meshgrid(t, t, indexing="ij")
h = np.exp(-(hx**2 + hy**2) / (2 * sigh**2))
h /= h.sum()

Bop = pylops.signalprocessing.Convolve2D(
    dims=(ny, nx), h=h, offset=(nh // 2, nh // 2), dtype="float64"
)

yclean = Bop @ im.ravel()
snr_db = 30.0
sigman = np.linalg.norm(yclean) / np.sqrt(yclean.size) * 10 ** (-snr_db / 20)
y = yclean + sigman * np.random.randn(yclean.size)

fig, axs = plt.subplots(1, 3, figsize=(12, 4))
axs[0].imshow(im, cmap="gray", vmin=0, vmax=1)
axs[0].set_title("True image")
axs[0].axis("off")
axs[1].imshow(y.reshape(ny, nx), cmap="gray", vmin=0, vmax=1)
axs[1].set_title(f"Blurred+noisy (PSNR={psnr(im, y.reshape(ny, nx), 1.0):.1f} dB)")
axs[1].axis("off")
axs[2].imshow(h, cmap="viridis")
axs[2].set_title(rf"Blurring kernel ($\sigma$={sigh})")
axs[2].axis("off")
plt.tight_layout()
True image, Blurred+noisy (PSNR=22.7 dB), Blurring kernel ($\sigma$=2.0)

Next, we define the two wavelet transforms and stack them vertically into the operator \(\mathbf{K}\).

W1op = pylops.signalprocessing.DWT2D(dims=(ny, nx), wavelet="haar", level=3)
W2op = pylops.signalprocessing.DWT2D(dims=(ny, nx), wavelet="db4", level=3)
Kop = pylops.VStack([W1op, W2op])
n1, n2 = W1op.shape[0], W2op.shape[0]

We are now ready to define the proximal operators. The data term is a pyproximal.L2 norm with the blurring operator, whilst the regularization term is a pyproximal.VStack of two pyproximal.L1 norms, each acting on the portion of the vector \(\mathbf{K}\mathbf{x}\) associated with one of the two wavelet transforms.

lam1 = 1e-3
lam2 = 1e-3

f = pyproximal.L2(Op=Bop, b=y)
g = pyproximal.VStack(
    [pyproximal.L1(sigma=lam1), pyproximal.L1(sigma=lam2)], nn=[n1, n2]
)

The Primal-Dual algorithm converges provided that \(\tau \mu \|\mathbf{K}\|_2^2 < 1\). Since each wavelet transform is (close to) orthogonal, we have \(\|\mathbf{K}\|_2^2 \le 2\), and choose the step lengths accordingly (with some safety margin). To appreciate the benefit of combining the two priors, we also solve the problem using a single wavelet transform at a time (for which \(\|\mathbf{W}_i\|_2^2 \le 1\)).

Lw = 1.0  # maxeig(W_i^H W_i)
L = 2.0  # maxeig(K^H K)

tau = 0.4
muw = 0.8 / (tau * Lw)
mu = 0.8 / (tau * L)
niter = 400
x0 = np.zeros(ny * nx)

# Haar only
xhaar = pyproximal.optimization.primaldual.PrimalDual(
    f, pyproximal.L1(sigma=lam1), W1op, tau=tau, mu=muw, x0=x0, niter=niter
)
xhaar = xhaar.reshape(ny, nx)

# db4 only
xdb4 = pyproximal.optimization.primaldual.PrimalDual(
    f, pyproximal.L1(sigma=lam2), W2op, tau=tau, mu=muw, x0=x0, niter=niter
)
xdb4 = xdb4.reshape(ny, nx)

# Haar + db4
xdual = pyproximal.optimization.primaldual.PrimalDual(
    f, g, Kop, tau=tau, mu=mu, x0=x0, niter=niter, show=True
)
xdual = xdual.reshape(ny, nx)
PrimalDual
-------------------------------------------------------------------------------------
Proximal operator (f): L2
Proximal operator (g): VStack
Linear operator (A): VStack
Additional vector (z): None

tau = 0.4       mu = 1.0        theta = 1.00e+00
tol = None      niter = 400
-------------------------------------------------------------------------------------

    Itn           x[0]                 f            g          z^x       J=f+g+z^x
     1         5.6800e-02          4.0099e+03  2.5153e+00   0.0000e+00   4.0124e+03
     2         1.0514e-01          2.1060e+03  4.3364e+00   0.0000e+00   2.1104e+03
     3         1.4626e-01          1.1148e+03  5.6790e+00   0.0000e+00   1.1205e+03
     4         1.8152e-01          5.9689e+02  6.6715e+00   0.0000e+00   6.0357e+02
     5         2.1197e-01          3.2509e+02  7.4092e+00   0.0000e+00   3.3250e+02
     6         2.3854e-01          1.8157e+02  7.9613e+00   0.0000e+00   1.8953e+02
     7         2.6180e-01          1.0517e+02  8.3778e+00   0.0000e+00   1.1355e+02
     8         2.8235e-01          6.4054e+01  8.6942e+00   0.0000e+00   7.2748e+01
     9         3.0074e-01          4.1593e+01  8.9363e+00   0.0000e+00   5.0529e+01
    10         3.1721e-01          2.9078e+01  9.1234e+00   0.0000e+00   3.8202e+01
    11         3.3208e-01          2.1923e+01  9.2698e+00   0.0000e+00   3.1193e+01
    21         4.2999e-01          9.5573e+00  9.8314e+00   0.0000e+00   1.9389e+01
    31         4.8834e-01          8.3332e+00  9.9744e+00   0.0000e+00   1.8308e+01
    41         5.3058e-01          7.8823e+00  1.0039e+01   0.0000e+00   1.7921e+01
    51         5.6430e-01          7.6548e+00  1.0074e+01   0.0000e+00   1.7729e+01
    61         5.9193e-01          7.5199e+00  1.0095e+01   0.0000e+00   1.7615e+01
    71         6.1643e-01          7.4315e+00  1.0108e+01   0.0000e+00   1.7540e+01
    81         6.3722e-01          7.3697e+00  1.0117e+01   0.0000e+00   1.7487e+01
    91         6.5415e-01          7.3247e+00  1.0123e+01   0.0000e+00   1.7447e+01
   101         6.6905e-01          7.2906e+00  1.0127e+01   0.0000e+00   1.7417e+01
   111         6.8379e-01          7.2640e+00  1.0129e+01   0.0000e+00   1.7393e+01
   121         6.9529e-01          7.2428e+00  1.0131e+01   0.0000e+00   1.7373e+01
   131         7.0486e-01          7.2254e+00  1.0132e+01   0.0000e+00   1.7357e+01
   141         7.1356e-01          7.2112e+00  1.0133e+01   0.0000e+00   1.7344e+01
   151         7.2171e-01          7.1992e+00  1.0134e+01   0.0000e+00   1.7333e+01
   161         7.2929e-01          7.1889e+00  1.0135e+01   0.0000e+00   1.7323e+01
   171         7.3648e-01          7.1802e+00  1.0135e+01   0.0000e+00   1.7315e+01
   181         7.4315e-01          7.1726e+00  1.0136e+01   0.0000e+00   1.7308e+01
   191         7.4913e-01          7.1659e+00  1.0136e+01   0.0000e+00   1.7302e+01
   201         7.5494e-01          7.1600e+00  1.0137e+01   0.0000e+00   1.7297e+01
   211         7.6050e-01          7.1548e+00  1.0137e+01   0.0000e+00   1.7292e+01
   221         7.6573e-01          7.1501e+00  1.0138e+01   0.0000e+00   1.7288e+01
   231         7.7066e-01          7.1459e+00  1.0138e+01   0.0000e+00   1.7284e+01
   241         7.7515e-01          7.1421e+00  1.0138e+01   0.0000e+00   1.7280e+01
   251         7.7940e-01          7.1386e+00  1.0138e+01   0.0000e+00   1.7277e+01
   261         7.8358e-01          7.1354e+00  1.0139e+01   0.0000e+00   1.7274e+01
   271         7.8781e-01          7.1326e+00  1.0139e+01   0.0000e+00   1.7271e+01
   281         7.9187e-01          7.1299e+00  1.0139e+01   0.0000e+00   1.7269e+01
   291         7.9586e-01          7.1275e+00  1.0139e+01   0.0000e+00   1.7266e+01
   301         7.9979e-01          7.1253e+00  1.0139e+01   0.0000e+00   1.7264e+01
   311         8.0362e-01          7.1232e+00  1.0139e+01   0.0000e+00   1.7262e+01
   321         8.0710e-01          7.1213e+00  1.0139e+01   0.0000e+00   1.7260e+01
   331         8.1022e-01          7.1195e+00  1.0139e+01   0.0000e+00   1.7258e+01
   341         8.1316e-01          7.1179e+00  1.0139e+01   0.0000e+00   1.7257e+01
   351         8.1602e-01          7.1163e+00  1.0139e+01   0.0000e+00   1.7255e+01
   361         8.1884e-01          7.1149e+00  1.0139e+01   0.0000e+00   1.7254e+01
   371         8.2157e-01          7.1135e+00  1.0139e+01   0.0000e+00   1.7252e+01
   381         8.2404e-01          7.1122e+00  1.0139e+01   0.0000e+00   1.7251e+01
   391         8.2632e-01          7.1111e+00  1.0139e+01   0.0000e+00   1.7250e+01
   392         8.2654e-01          7.1109e+00  1.0139e+01   0.0000e+00   1.7250e+01
   393         8.2675e-01          7.1108e+00  1.0139e+01   0.0000e+00   1.7249e+01
   394         8.2697e-01          7.1107e+00  1.0139e+01   0.0000e+00   1.7249e+01
   395         8.2718e-01          7.1106e+00  1.0139e+01   0.0000e+00   1.7249e+01
   396         8.2740e-01          7.1105e+00  1.0139e+01   0.0000e+00   1.7249e+01
   397         8.2761e-01          7.1104e+00  1.0139e+01   0.0000e+00   1.7249e+01
   398         8.2782e-01          7.1103e+00  1.0139e+01   0.0000e+00   1.7249e+01
   399         8.2803e-01          7.1102e+00  1.0139e+01   0.0000e+00   1.7249e+01
   400         8.2824e-01          7.1100e+00  1.0139e+01   0.0000e+00   1.7249e+01

Iterations = 400        Total time (s) = 11.60
-------------------------------------------------------------------------------------

Finally, we compare the three reconstructions.

fig, axs = plt.subplots(2, 2, figsize=(9, 9))
for ax, img, title in zip(
    axs.ravel(),
    [y.reshape(ny, nx), xhaar, xdb4, xdual],
    ["Blurred+noisy", "Haar prior", "db4 prior", "Haar + db4 priors"],
    strict=True,
):
    ax.imshow(img, cmap="gray", vmin=0, vmax=1)
    ax.set_title(f"{title} (PSNR={psnr(im, img, 1.0):.1f} dB)")
    ax.axis("off")
plt.tight_layout()
Blurred+noisy (PSNR=22.7 dB), Haar prior (PSNR=24.8 dB), db4 prior (PSNR=26.3 dB), Haar + db4 priors (PSNR=26.5 dB)

Both single-prior reconstructions already remove most of the blur. The Haar prior favours piecewise-constant solutions and is therefore prone to blocky artefacts, whilst the db4 prior produces smoother images. By combining the two priors, we obtain a reconstruction that benefits from the strengths of both transforms.

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

Gallery generated by Sphinx-Gallery