# Image Deconvolution using Variational Method¶

This numerical tour explores the use of Sobolev and TV regularization to perform image deconvolution.


In [2]:
options(warn=-1) # turns off warnings, to turn on: "options(warn=0)"
library(pracma)
library(imager)

# Importing the libraries
for (f in list.files(path="nt_toolbox/toolbox_general/", pattern="*.R")) {
source(paste("nt_toolbox/toolbox_general/", f, sep=""))
}
for (f in list.files(path="nt_toolbox/toolbox_signal/", pattern="*.R")) {
source(paste("nt_toolbox/toolbox_signal/", f, sep=""))
}


This tour is concerned with the deconvolution problem. The measurement are assumed to be blurry and noisy: $$y=\Phi f_0 + w = h \star f_0 + w$$

Where here |h| is the filter (low pass) and |w| some noise (here assumed to be white Gaussian).

We consider variational deconvolution methods, that finds a regularizer through a convex optimization: $$f^\star \in \text{argmin}_f \frac{1}{2}\|y-\Phi f\|^2 + \lambda J(f)$$

Where $J(f)$ is a prior energy. In this tour we consider a simple L2 prior (the image is assumed to have a bounded energy), a Sobolev prior (the image is uniformly smooth) and an approximate total variation (the image has edges of bounded perimeter).

Note that the parameter $\lambda$ should be carefully chosen to fit the noise level.

## Image Blurring¶

Deconvolution corresponds to removing a blur from an image. We use here a Gaussian blur.

First we load the image to be inpainted.

In [2]:
n = 256
name = 'nt_toolbox/data/hibiscus.png'


Initial image $f_0$.

In [3]:
options(repr.plot.width=4, repr.plot.height=4)
imageplot(f0, 'Image f0')


We build a convolution kernel. Since we are going to use Fourier to compute the convolution, we set the center of the kernel in the (1,1) pixel location.

Width $s$ of the kernel, in pixel.

In [4]:
s = 3


Define the convolution kernel $h$.

In [5]:
x = c(c(0:((n/2) - 1)), c(-(n/2):-1))
mesh = meshgrid(x)
X = mesh$X Y = mesh$Y
h = exp( -(X**2 + Y**2)/(2.0 * s**2) )
h = h/sum(h)


Useful for later : the Fourier transform (should be real because of symmetry).

In [6]:
hF = Re(fft(h))


Display the kernel and its transform. We use |fftshift| to center the filter for display.

In [7]:
imageplot(fftshift(h), 'Filter', c(1,2,1))
imageplot(fftshift(hF), 'Fourier transform', c(1,2,2))


We use this short hand for the filtering. Note that this is a symmetric operator.

In [8]:
Phi = function(x,h){Re(fft(fft(x) * fft(h), inverse=TRUE) / length(h))}


Apply the filter.

In [9]:
y0 = Phi(f0[,], h)


Display the filtered observation.

In [10]:
imageplot(y0, 'Observation without noise')


Variance $\sigma^2$ of the noise $w$.

In [11]:
sigma = .02


Add some noise to obtain the measurements $y = \Phi f_0 + w$.

In [12]:
set.seed(123)
y = y0 + randn(n,n) * sigma


Display.

In [13]:
imageplot(y, 'Observation with noise')

In [14]:
# Normalize to 0-1
imageplot(y, paste('Observation, SNR = ',round(snr(f0, y),2),'dB'))


## Deconvolution with L2 Regularization¶

Deconvolution is obtained by dividing the Fourier transform of $y$ by $\hat h$. $$f^\star(\omega) = \frac{\hat y(\omega)}{\hat h(\omega)} = \hat f_0(\omega) + \hat w(\omega)/{\hat h(\omega)}$$

Unfortunately this creates an explosion of the Noise.

To avoid this explosion, we consider a simple regularization. $$f^{\star} = \text{argmin}_f \: \|y-\Phi f\|^2 + \lambda \|f\|^2$$

Since the filtering is diagonalized over Fourier, the solution is simply computed over the Fourier domain as: $$\hat f^\star(\omega) = \frac{\hat y(\omega) \hat h(\omega)}{ \|\hat h(\omega)\|^2 + \lambda }$$

Useful for later: Fourier transform of the observations.

In [15]:
yF = fft(y)


Select a value for the regularization parameter.

In [16]:
Lambda = 0.02


Perform the inversion.

In [17]:
fL2 = Re(fft(yF * hF / (abs(hF)**2 + Lambda), inverse=TRUE)) / length(yF * hF / (abs(hF)**2 + Lambda))


Display.

In [18]:
imageplot(fL2, paste('L2 deconvolution, SNR = ', round(snr(f0, fL2),2), 'dB'))


Exercise 1: Find the optimal solution fL2 by testing several value of $\lambda$.

In [19]:
source("nt_solutions/inverse_2_deconvolution_variation/exo1.R")

In [20]:
# Insert your code here.


Display optimal result.

In [21]:
# Insert your code here.


## Deconvolution by Sobolev Regularization.¶

L2 regularization did not perform any denoising. To remove some noise, we can penalize high frequencies using Sobolev regularization (quadratic grow).

The Sobolev prior reads (note the conversion from spacial domain to Fourier domain) $$J(f) = \sum_x \|\nabla f(x)\|^2 = \sum_{\omega} S(\omega) \|\hat f(\omega)\|^2$$ where $S(\omega)=\|\omega\|^2$.

Since this prior can be written over the Fourier domain, one can compute the solution to the deblurring with Sobolev prior simply with the Fourier coefficients: $$\hat f^\star(\omega) = \frac{\hat y(\omega) \hat h(\omega)}{ \|\hat h(\omega)\|^2 + \lambda S(\omega) }$$

Compute the Sobolev prior penalty |S|(rescale to [0,1]).

In [22]:
S = (X**2 + Y**2) * (2/n)**2


Regularization parameter:

In [23]:
Lambda = 0.2


Perform the inversion.

In [24]:
fSob = Re(fft(yF * hF / (abs(hF)**2 + Lambda*S), inverse=TRUE)) / length(yF * hF / (abs(hF)**2 + Lambda*S))


Display the result.

In [25]:
imageplot(fSob, paste('Sobolev deconvolution, SNR = ', round(snr(f0, fSob),2), 'dB'))


Exercise 2: Find the optimal solution fSob by testing several value of $\lambda$.

In [26]:
source("nt_solutions/inverse_2_deconvolution_variation/exo2.R")

In [27]:
# Insert your code here.


Display optimal result.

In [28]:
# Insert your code here.


## Deconvolution by Total Variation Regularization¶

Sobolev regularization perform a denoising but also tends to blur the edges, thus producing a poor results on cartoon images.

The TV prior is able to better reconstruct sharp edges. It reads: $$J(f) = \sum_x \| \nabla f(x)\|$$

With respect to the Sobolev energy, it simply corresponding to measuring the L1 norm instead of the L2 norm, thus dropping the square in the functional.

Unfortunately, the TV functional $J(f)$ is not a smooth function of the image $f$. It thus requires the use of advanced convex optimization method to be minimized for regularization.

An alternative is to replace the absolute value by a smooth absolute value. The smoothed TV norm reads: $$J(f) = \sum_x \sqrt{\|\nabla f(x)\|^2+\varepsilon^2}$$

Regularization parameter for the TV norm:

In [29]:
epsilon = 0.4*1e-2


When $\epsilon$ gets close to zero, the smoothed energy becomes closer to the original total variation, but the optimization becomes more difficult. When |epsilon| becomes large, the smoothed energy becomes closer to the Sobolev energy, thus blurring the edges.

Unfortunately, this prior is non-quadratic, and cannot be expressed over the Fourier domain. One thus need to use an iterative scheme such as a gradient descent to approximate the solution.

An iteration of the gradient descent reads: $$f^{(k+1)} = f^{(k)} - \tau \left( h \star (h \star f^{(k)} - y) + \lambda \text{Grad} J(f^{(k)}) \right)$$

Regularization parameter.

In [30]:
Lambda = 0.06


The value of $\tau$, the step size, should be smaller than twice the Lipschitz constant of the Gradient of the functional to be minimized, hence: $$\tau< \frac{2}{1 + \lambda 8/\varepsilon }.$$

In [31]:
tau = 1.9 / (1 + Lambda * 8 / epsilon)


Initialization.

In [32]:
fTV = y


Number of iteration (quite a large number is required).

In [33]:
niter = 300


We first check that the discretized grad and -div are adjoint one of each other.

In [34]:
#importing grad and div
div = div_2

a = randn(n,n)
b = array(0, dim=c(n,n,2))
b[,,1] = randn(n, n)
b[,,2] = randn(n, n)
dotp = function(x,y){sum(x * y)}
print(paste("Should be 0: ", dotp(grad(a),b) + dotp(a,div(b))))

[1] "Should be 0:  0"


The gradient of the smoothed TV energy is: $$\text{Grad} J(f) = -\text{div}\left( \frac{\nabla f}{ \sqrt{\|\nabla f\|^2+\varepsilon^2} } \right)$$

Compute the gradient of the smoothed TV functional.

In [35]:
Gr = grad(fTV)
d = sqrt(epsilon**2 + (Gr**2)[,,1] + (Gr**2)[,,2])
G = -div(Gr / array(rep(d, 2), dim=c(dim(d),2)) )


Compute the TV norm, usefull to keep track of its decay through iterations.

In [36]:
tv = sum(d)
print(paste('Smoothed TV norm of the image: ', round(tv,2)))

[1] "Smoothed TV norm of the image:  2493.94"


Perform a step of gradient descent for the inversion.

In [37]:
e = Phi(fTV, h) - y
fTV = fTV - tau * (Phi(e, h) + Lambda * G)


Exercise 3: Perform the deblurring by a gradient descent. Keep track of the function being minimized.

In [38]:
source("nt_solutions/inverse_2_deconvolution_variation/exo3.R")

In [39]:
# Insert your code here.


Display the result.

In [40]:
# Insert your code here.


### Exercise 4¶

Explore the different values of |lambda| to find the optimal solution. Display the SNR as a function of |lambda|.

In [ ]:
source("nt_solutions/inverse_2_deconvolution_variation/exo4.R")

In [ ]:
# Insert your code here.


Display the result.

In [ ]:
# Insert your code here.