This numerical tour explores the use of Sobolev and TV regularization to perform image deconvolution.
Important: Please read the installation page for details about how to install the toolboxes. $\newcommand{\dotp}[2]{\langle #1, #2 \rangle}$ $\newcommand{\enscond}[2]{\lbrace #1, #2 \rbrace}$ $\newcommand{\pd}[2]{ \frac{ \partial #1}{\partial #2} }$ $\newcommand{\umin}[1]{\underset{#1}{\min}\;}$ $\newcommand{\umax}[1]{\underset{#1}{\max}\;}$ $\newcommand{\umin}[1]{\underset{#1}{\min}\;}$ $\newcommand{\uargmin}[1]{\underset{#1}{argmin}\;}$ $\newcommand{\norm}[1]{\|#1\|}$ $\newcommand{\abs}[1]{\left|#1\right|}$ $\newcommand{\choice}[1]{ \left\{ \begin{array}{l} #1 \end{array} \right. }$ $\newcommand{\pa}[1]{\left(#1\right)}$ $\newcommand{\diag}[1]{{diag}\left( #1 \right)}$ $\newcommand{\qandq}{\quad\text{and}\quad}$ $\newcommand{\qwhereq}{\quad\text{where}\quad}$ $\newcommand{\qifq}{ \quad \text{if} \quad }$ $\newcommand{\qarrq}{ \quad \Longrightarrow \quad }$ $\newcommand{\ZZ}{\mathbb{Z}}$ $\newcommand{\CC}{\mathbb{C}}$ $\newcommand{\RR}{\mathbb{R}}$ $\newcommand{\EE}{\mathbb{E}}$ $\newcommand{\Zz}{\mathcal{Z}}$ $\newcommand{\Ww}{\mathcal{W}}$ $\newcommand{\Vv}{\mathcal{V}}$ $\newcommand{\Nn}{\mathcal{N}}$ $\newcommand{\NN}{\mathcal{N}}$ $\newcommand{\Hh}{\mathcal{H}}$ $\newcommand{\Bb}{\mathcal{B}}$ $\newcommand{\Ee}{\mathcal{E}}$ $\newcommand{\Cc}{\mathcal{C}}$ $\newcommand{\Gg}{\mathcal{G}}$ $\newcommand{\Ss}{\mathcal{S}}$ $\newcommand{\Pp}{\mathcal{P}}$ $\newcommand{\Ff}{\mathcal{F}}$ $\newcommand{\Xx}{\mathcal{X}}$ $\newcommand{\Mm}{\mathcal{M}}$ $\newcommand{\Ii}{\mathcal{I}}$ $\newcommand{\Dd}{\mathcal{D}}$ $\newcommand{\Ll}{\mathcal{L}}$ $\newcommand{\Tt}{\mathcal{T}}$ $\newcommand{\si}{\sigma}$ $\newcommand{\al}{\alpha}$ $\newcommand{\la}{\lambda}$ $\newcommand{\ga}{\gamma}$ $\newcommand{\Ga}{\Gamma}$ $\newcommand{\La}{\Lambda}$ $\newcommand{\si}{\sigma}$ $\newcommand{\Si}{\Sigma}$ $\newcommand{\be}{\beta}$ $\newcommand{\de}{\delta}$ $\newcommand{\De}{\Delta}$ $\newcommand{\phi}{\varphi}$ $\newcommand{\th}{\theta}$ $\newcommand{\om}{\omega}$ $\newcommand{\Om}{\Omega}$
using PyPlot
using NtToolBox
# using Autoreload
# arequire("NtToolBox")
WARNING: Method definition ndgrid(AbstractArray{T<:Any, 1}) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:3 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:3. WARNING: Method definition ndgrid(AbstractArray{#T<:Any, 1}, AbstractArray{#T<:Any, 1}) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:6 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:6. WARNING: Method definition ndgrid_fill(Any, Any, Any, Any) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:13 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:13. WARNING: Method definition ndgrid(AbstractArray{#T<:Any, 1}...) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:19 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:19. WARNING: Method definition meshgrid(AbstractArray{T<:Any, 1}) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:33 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:33. WARNING: Method definition meshgrid(AbstractArray{#T<:Any, 1}, AbstractArray{#T<:Any, 1}) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:36 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:36. WARNING: Method definition meshgrid(AbstractArray{#T<:Any, 1}, AbstractArray{#T<:Any, 1}, AbstractArray{#T<:Any, 1}) in module NtToolBox at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:44 overwritten at /Users/gpeyre/.julia/v0.5/NtToolBox/src/ndgrid.jl:44.
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.
Deconvolution corresponds to removing a blur from an image. We use here a Gaussian blur.
First we load the image to be inpainted.
n = 256
name = "NtToolBox/src/data/hibiscus.png"
f0 = load_image(name, n);
Initial image $f_0$.
imageplot(f0, "Image f0", [1, 2, 1])
PyObject <matplotlib.text.Text object at 0x321b5aa50>
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.
s = 3;
Define the convolution kernel $h$.
include("NtToolBox/src/ndgrid.jl")
x = [collect(0:n/2 - 1); collect(-n/2:-1)]
(Y, X) = meshgrid(x, x)
h = exp((-X.^2 - Y.^2)/(2*s^2))
h = h/sum(h);
Useful for later : the Fourier transform (should be real because of symmetry).
hF = real(plan_fft(h)*h);
Display the kernel and its transform. We use |fftshift| to center the filter for display.
imageplot(fftshift(h), "Filter", [1, 2, 1])
imageplot(fftshift(hF), "Fourier transform", [1, 2, 2])
PyObject <matplotlib.text.Text object at 0x3225ec810>
We use this short hand for the filtering. Note that this is a symmetric operator.
Phi = (x, h) -> real(plan_ifft((plan_fft(x)*x) .* (plan_fft(h)*h))*((plan_fft(x)*x) .* (plan_fft(h)*h)))
(::#3) (generic function with 1 method)
Important Scilab user should define a function |Phi| in a separate file |Phi.sci| to perform this.
Apply the filter.
y0 = Phi(f0, h);
Display the filtered observation.
imageplot(y0, "Observation without noise", [1, 2, 2])
PyObject <matplotlib.text.Text object at 0x322740b50>
Variance $\sigma^2$ of the noise $w$.
sigma = .02;
Add some noise to obtain the measurements $y = \Phi f_0 + w$.
y = y0 .+ randn(n, n).*sigma;
Display.
imageplot(clamP(y), "Observation with noise", [1, 2, 2])
PyObject <matplotlib.text.Text object at 0x3273cdc10>
imageplot(y, string("Observation, SNR = ", string(round(snr(f0, y),2)), "dB"))
PyObject <matplotlib.text.Text object at 0x327929d50>
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.
yF = plan_fft(y)*y;
Select a value for the regularization parameter.
Lambda = 0.02;
Perform the inversion.
fL2 = real(plan_ifft((yF .* hF ./ (abs(hF).^2 .+ Lambda)))*((yF .* hF ./ (abs(hF).^2 .+ Lambda))));
Display.
imageplot(clamP(fL2), string("L2 deconvolution, SNR = ", string(round(snr(f0, fL2), 2)), "dB"))
PyObject <matplotlib.text.Text object at 0x322b0ad10>
Exercise 1: Find the optimal solution fL2 by testing several value of $\lambda$.
include("NtSolutions/inverse_2_deconvolution_variational/exo1.jl")
256×256 Array{Float64,2}: 0.156594 0.169293 0.168146 0.159362 … 0.0569203 0.0927654 0.129255 0.149093 0.163959 0.164797 0.157759 0.0463649 0.0821585 0.119799 0.141592 0.160978 0.167566 0.166534 0.0377254 0.0718776 0.109761 0.135579 0.160979 0.175731 0.183302 0.0318073 0.0634383 0.100923 0.133129 0.164734 0.188031 0.204305 0.0303445 0.0592057 0.095854 0.135688 0.17258 0.203008 0.225877 … 0.0345144 0.0607912 0.0963694 0.143229 0.18401 0.219253 0.245518 0.0437037 0.0678691 0.102452 0.154222 0.19757 0.235291 0.261861 0.0555449 0.0782747 0.112298 0.166222 0.210975 0.249142 0.273632 0.0671055 0.0891791 0.123286 0.176591 0.221361 0.257976 0.278566 0.0762521 0.0983751 0.133028 0.182964 0.225756 0.258507 0.273535 … 0.0822747 0.104815 0.139863 0.183514 0.221877 0.248343 0.256318 0.0855897 0.108285 0.142719 0.177273 0.209136 0.227704 0.227965 0.0869963 0.10876 0.140857 ⋮ ⋱ ⋮ 0.22275 0.259751 0.285882 0.295807 0.122307 0.146767 0.18245 0.209855 0.248092 0.277166 0.292069 … 0.110899 0.134412 0.169491 0.201534 0.239417 0.269481 0.287324 0.104609 0.127752 0.162057 0.200536 0.236726 0.2659 0.284429 0.105644 0.129079 0.162628 0.204856 0.238407 0.265482 0.283219 0.113219 0.136849 0.169212 0.209437 0.239824 0.264344 0.280821 0.123283 0.146341 0.176727 0.209616 0.236685 0.25856 0.273746 … 0.130121 0.152094 0.1801 0.203775 0.227607 0.246547 0.259971 0.129184 0.150691 0.176995 0.193605 0.214314 0.229517 0.239825 0.11941 0.142205 0.1685 0.182284 0.199946 0.210359 0.215724 0.103465 0.129492 0.157669 0.172231 0.187098 0.192202 0.191462 0.0857808 0.116009 0.147219 0.163974 0.176895 0.177568 0.171376 … 0.0698755 0.103762 0.138024
Display optimal result.
imageplot(clamP(fL2), string("L2 deconvolution, SNR = ", string(round(snr(f0, fL2), 2)), "dB") )
PyObject <matplotlib.text.Text object at 0x327ceed10>
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]).
S = (X.^2 + Y.^2) .* (2/n).^2;
Regularization parameter:
Lambda = 0.2;
Perform the inversion.
fSob = real(plan_ifft((yF .* hF ./ (abs(hF).^2 .+ Lambda.*S)))*((yF .* hF ./ (abs(hF).^2 .+ Lambda.*S))));
Display the result.
imageplot(clamP(fSob), string("Sobolev deconvolution, SNR = ", string( round(snr(f0, fSob), 2) ), "dB") )
PyObject <matplotlib.text.Text object at 0x326d5b750>
Exercise 2: Find the optimal solution fSob by testing several value of $\lambda$.
include("NtSolutions/inverse_2_deconvolution_variational/exo2.jl")
256×256 Array{Float64,2}: 0.150678 0.165293 0.16828 0.163847 … 0.0606054 0.0918958 0.124577 0.141715 0.15862 0.164072 0.161996 0.048801 0.0801691 0.113833 0.135178 0.156389 0.167182 0.170493 0.0396928 0.0703111 0.104697 0.131841 0.158721 0.176736 0.187271 0.0337691 0.0632204 0.098197 0.132592 0.165503 0.19113 0.208968 0.0319163 0.0601446 0.0956466 0.137949 0.176264 0.208429 0.231973 … 0.0348513 0.0620222 0.0980008 0.147563 0.189962 0.226593 0.253178 0.0424224 0.0687954 0.105211 0.159993 0.204816 0.243431 0.2701 0.0533392 0.079182 0.115992 0.172862 0.218318 0.256436 0.280541 0.0655072 0.0910215 0.1281 0.183325 0.227526 0.262844 0.282306 0.0767137 0.101937 0.138918 0.188723 0.229713 0.260203 0.27358 … 0.0852597 0.109947 0.146087 0.187277 0.223269 0.247407 0.253997 0.0902817 0.113822 0.147997 0.178662 0.208513 0.225713 0.225775 0.0917427 0.113178 0.144107 ⋮ ⋱ ⋮ 0.224411 0.262077 0.288803 0.299183 0.12456 0.148196 0.183681 0.213493 0.252996 0.283069 0.298515 … 0.113236 0.13648 0.172017 0.205371 0.245261 0.277179 0.296283 0.106988 0.129569 0.164362 0.202524 0.241302 0.273265 0.294065 0.107569 0.129523 0.163041 0.204346 0.24076 0.271162 0.29182 0.113936 0.135352 0.167224 0.207777 0.240969 0.268646 0.287795 0.122499 0.143489 0.173529 0.209131 0.238662 0.262845 0.279527 … 0.128586 0.149473 0.177854 0.205972 0.231728 0.251857 0.26525 0.12848 0.149958 0.177322 0.19798 0.220071 0.235685 0.244959 0.120958 0.143978 0.171303 0.186585 0.205318 0.216198 0.220728 0.107499 0.132876 0.16112 0.173868 0.18988 0.196416 0.19628 0.0911615 0.119146 0.148897 0.161566 0.176008 0.179566 0.176026 … 0.0749284 0.105056 0.136403
Display optimal result.
imageplot(clamP(fSob), string("Sobolev deconvolution, SNR = ", string( round(snr(f0, fSob), 2) ), "dB") )
PyObject <matplotlib.text.Text object at 0x3227efcd0>
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:
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.
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 }.$$
tau = 1.9 / (1 + Lambda * 8 / epsilon);
Initialization.
fTV = y;
Number of iteration (quite a large number is required).
niter = 300;
We first check that the discretized grad and -div are adjoint one of each other.
a = rand(n,n)
b = rand(n, n, 2)
dotp = (x, y) -> sum(x[:].*y[:])
print(string("Should be 0: ", string(dotp(grad(a),b) + dotp(a, NtToolBox.div(b)))) ) #We use NtToolBox.div instead of div because otherwise there will be a conflict with the function div of package Base.
Should be 0: 6.394884621840902e-14
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.
repeat3 = x -> cat(3, x, x)
Gr = grad(fTV)
d = sqrt(epsilon^2 .+ sum(Gr.^2, 3))[:, :]
G = -NtToolBox.div(Gr ./ repeat3(d) ); #We use NtToolBox.div instead of div because otherwise there will be a conflict with the function div of package Base.
Compute the TV norm, usefull to keep track of its decay through iterations.
tv = sum(d)
print(string("Smoothed TV norm of the image: ", string(round(tv,2))) )
Smoothed TV norm of the image: 2493.06
Perform a step of gradient descent for the inversion.
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.
include("NtSolutions/inverse_2_deconvolution_variational/exo3.jl")
PyObject <matplotlib.text.Text object at 0x32265c190>
Display the result.
imageplot(clamP(fTV))
Explore the different values of |lambda| to find the optimal solution. Display the SNR as a function of |lambda|.
include("NtSolutions/inverse_2_deconvolution_variational/exo4.jl")
256×256 Array{Float64,2}: 0.161024 0.16819 0.172161 0.175061 … 0.0520367 0.0730027 0.123809 0.158083 0.16653 0.17101 0.174265 0.0414933 0.0540402 0.107165 0.156025 0.16623 0.171542 0.175486 0.0364862 0.0427696 0.0902067 0.155171 0.167463 0.174083 0.179402 0.034604 0.0388925 0.0766326 0.15604 0.170793 0.180091 0.197414 0.0344453 0.0389783 0.0706717 0.159203 0.177776 0.208928 0.235932 … 0.0357925 0.0422343 0.0753079 0.165177 0.203996 0.244026 0.247117 0.0392051 0.0501487 0.0891905 0.17847 0.23811 0.248462 0.251263 0.0467249 0.0639976 0.106346 0.196628 0.243452 0.250716 0.253147 0.0606082 0.0790638 0.122873 0.206067 0.244117 0.251273 0.253445 0.0750607 0.0907164 0.136869 0.20617 0.241643 0.250229 0.252257 … 0.0848945 0.0983425 0.144473 0.197663 0.234566 0.247066 0.249208 0.0908091 0.102323 0.14384 0.179577 0.217204 0.238883 0.242565 0.0940729 0.103369 0.13591 ⋮ ⋱ ⋮ 0.238312 0.27072 0.275918 0.27797 0.137637 0.142806 0.165622 0.228943 0.271395 0.277396 0.279813 … 0.132908 0.13662 0.15091 0.220064 0.270892 0.27798 0.280711 0.129741 0.133034 0.142414 0.216461 0.269294 0.277745 0.280785 0.127885 0.13164 0.142151 0.21804 0.266237 0.276553 0.280015 0.126882 0.131495 0.149466 0.218909 0.260866 0.273948 0.278216 0.126402 0.132583 0.160527 0.214793 0.251677 0.268762 0.274873 … 0.125905 0.134919 0.168141 0.205025 0.236342 0.257686 0.268181 0.124258 0.136765 0.168973 0.191211 0.2133 0.2326 0.245848 0.119336 0.135048 0.164463 0.178059 0.189933 0.200178 0.208709 0.108587 0.12784 0.157222 0.169818 0.177108 0.182145 0.186406 0.0908557 0.114411 0.148637 0.164797 0.171374 0.175268 0.178214 … 0.0696499 0.0950231 0.137823
Display the result.
imageplot(clamP(fBest), string("TV deconvolution, SNR = ", string( round(snr(f0, fBest), 2) ), "dB") )
PyObject <matplotlib.text.Text object at 0x327c16450>