Laboratorio di Problemi Inversi Esercitazione 2: filtraggio spettrale Luca Calatroni Dipartimento di Matematica, Universitá degli studi di Genova Aprile 2016. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 1 / 17
Outline 1 Exploiting PSF structure for BCCB matrix computation 2 TSVD and Tikhonov implementation 3 Tikhonov parameter choice via GCV Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 2 / 17
A structural way to build blur operators Load the Shepp-Logan phantom im1=phantom( Modified Shepp-Logan, 256); Generate Gaussian PSF of the same size, σ 2 = 3 to blur the image. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 3 / 17
A structural way to build blur operators Load the Shepp-Logan phantom im1=phantom( Modified Shepp-Logan, 256); Generate Gaussian PSF of the same size, σ 2 = 3 to blur the image. We want to exploit the structure of the PSF and the type of boundary conditions considered to build efficiently the blur matrix B (similar to how imfilter works). Easy case: generic PSF, periodic boundary conditions BCCB matrix: starting from PSF, elements are circularly shifted with respect to the centre of the PSF... see theory! Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 3 / 17
A structural way to build blur operators Load the Shepp-Logan phantom im1=phantom( Modified Shepp-Logan, 256); Generate Gaussian PSF of the same size, σ 2 = 3 to blur the image. We want to exploit the structure of the PSF and the type of boundary conditions considered to build efficiently the blur matrix B (similar to how imfilter works). Easy case: generic PSF, periodic boundary conditions BCCB matrix: starting from PSF, elements are circularly shifted with respect to the centre of the PSF... see theory! Determine the centre of Gaussian PSF. Use circshift to build first column of the blur matrix B (still a matrix). Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 3 / 17
Why all this? Can t we use MATLAB imfilter? Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 4 / 17
Why all this? Can t we use MATLAB imfilter? Need for inversion! No blur operator provided by MATLAB when using fspecial, only PSF and filtered results! From an inverse problem perspective, the operator B (needed for inversion) is not provided by MATLAB. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 4 / 17
Why all this? Can t we use MATLAB imfilter? Need for inversion! No blur operator provided by MATLAB when using fspecial, only PSF and filtered results! From an inverse problem perspective, the operator B (needed for inversion) is not provided by MATLAB. The spectral properties of B can further be exploited for quick implementation!... and actually the computation of B is never directly required! but... only the first column is needed! Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 4 / 17
Notation For what follows: denote by σ i, i = 1,..., N the (complex) eigenvalues of B diagonal matrix Σ; F the 2-dimensional Fourier transform matrix; F the 2-dimensional anti-fourier transform matrix. MATLAB: use fft2 and ifft2 as (rescaled) matrix multiplication by F and F, respectively.... Vectorisation! y=bx... Warning: fft2 and ifft2 act on 2D arrays. So, from here onwards 1 First apply the functions to matrices; 2 Then, reshape. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 5 / 17
Useful properties of BBCB matrices 1 The set of eigenvectors of every BCCB matrix can be expressed in terms of the 2D-Fourier transform: B = F ΣF, which entails: FB = ΣF Fb 1 = Σf 1 where b 1 and f 1 are the first columns of B and F matrix, respectively. We have: 1 f 1 = 1 1 N. 1 Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 6 / 17
Useful properties of BBCB matrices 1 The set of eigenvectors of every BCCB matrix can be expressed in terms of the 2D-Fourier transform: B = F ΣF, which entails: FB = ΣF Fb 1 = Σf 1 = 1 N σ where b 1 and f 1 are the first columns of B and F matrix, respectively. We have: 1 f 1 = 1 1 N. 1 2 to compute σ, the eigenvalues of B, apply fft2 to first column of blur matrix (which comes, unintuitively, as a matrix), then vectorise the result. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 6 / 17
Why all this? We will...... create MATLAB functions that will serve as regularised solvers for the deblurring problem. Input? We will focus on Tikhonov and TSVD, both implemented by means of the σ i. Do it wisely! Using this strategy based on the use of fft2 reduces computational costs with respect to using the eig function. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 7 / 17
Why all this? We will...... create MATLAB functions that will serve as regularised solvers for the deblurring problem. Input? We will focus on Tikhonov and TSVD, both implemented by means of the σ i. Do it wisely! Using this strategy based on the use of fft2 reduces computational costs with respect to using the eig function. Step 0: use all this to create the function naive deblur to compute deblurred version of im1 using: x = B 1 y = F Σ 1 Fy Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 7 / 17
Why all this? We will...... create MATLAB functions that will serve as regularised solvers for the deblurring problem. Input? We will focus on Tikhonov and TSVD, both implemented by means of the σ i. Do it wisely! Using this strategy based on the use of fft2 reduces computational costs with respect to using the eig function. Step 0: use all this to create the function naive deblur to compute deblurred version of im1 using: x = B 1 y = F Σ 1 Fy - compute eigenvalues as above. - standard Fourier inversion (take the real part). - we know it won t work! Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 7 / 17
Outline 1 Exploiting PSF structure for BCCB matrix computation 2 TSVD and Tikhonov implementation 3 Tikhonov parameter choice via GCV Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 8 / 17
Let s take a look at the SVD... Let B bw BCCB and let us introduce Φ, filters (like the ones we saw last time for thresholding/regularisation), we have: x = B 1 y = F ΦΣ 1 Fy Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 9 / 17
Let s take a look at the SVD... Let B bw BCCB and let us introduce Φ, filters (like the ones we saw last time for thresholding/regularisation), we have: Towards filtering... x = B 1 y = F ΦΣ 1 Fy refine naive deb giving Φ as input. In the naive case, what is Φ? vectorise Σ and blurred image as σ, y, respectively. introduce auxiliary filtering vector s filt =... and invert by multiplying. remember always to reshape before applying fft2, ifft2 and to take the real part. Output: deblurred image. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 9 / 17
The problem x = B 1 y = F ΦΣ 1 Fy Informally, when inverting with MATLAB: Phi=speye(size(im)); phi=diag(phi); deb=ifft2((phi./s).* "fft2(b)") Note the (possible) division by 0! Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 10 / 17
Truncated Singular Value Decomposition filtering Idea: removing small singular values of σ by filtering them by multiplication by 0. { 1, i = 1,..., k, φ i = 0, i = k + 1,..., N. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 11 / 17
Truncated Singular Value Decomposition filtering Idea: removing small singular values of σ by filtering them by multiplication by 0. { 1, i = 1,..., k, φ i = 0, i = k + 1,..., N. Task: modify Φ to eliminate small singular values. Write function tsvd fft2 with the same input as before with additional tol=0.01 value to threshold. Set small elements of σ to 0. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 11 / 17
Truncated Singular Value Decomposition filtering Idea: removing small singular values of σ by filtering them by multiplication by 0. { 1, i = 1,..., k, φ i = 0, i = k + 1,..., N. Task: modify Φ to eliminate small singular values. Write function tsvd fft2 with the same input as before with additional tol=0.01 value to threshold. Set small elements of σ to 0. Use find to compute the vectors of the non-null elements of Φ. For these elements, compute the filtering, reshape, use ifft2 to compute the output deblurred image. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 11 / 17
TSVD: take-home message and variations on the theme Problem solved!! Observe that the problems of the inversion are solved by the multiplication of filters which never blow up (after thresholding)! Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 12 / 17
TSVD: take-home message and variations on the theme Problem solved!! Observe that the problems of the inversion are solved by the multiplication of filters which never blow up (after thresholding)! 1 1st variation: verify the robustness of the approach after adding noise? 2 2nd variation: how does the choice of tol affect the deblurring result? Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 12 / 17
Tikhonov filtering Idea: correct Φ by imposing that the denominator never vanishes. φ i = σ2 i σ 2 i + λ, λ > 0, where λ is the Tikhonov regularisation parameter. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 13 / 17
Tikhonov filtering Idea: correct Φ by imposing that the denominator never vanishes. φ i = σ2 i σ 2 i + λ, λ > 0, where λ is the Tikhonov regularisation parameter. Task: write Tikhonov filtering function. Write function tik fft2 with the same input as before with additional λ=0.01 regularisation parameter.. Write the filters appropriately... careful: do not divide by zero! Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 13 / 17
Tikhonov filtering Idea: correct Φ by imposing that the denominator never vanishes. φ i = σ2 i σ 2 i + λ, λ > 0, where λ is the Tikhonov regularisation parameter. Task: write Tikhonov filtering function. Write function tik fft2 with the same input as before with additional λ=0.01 regularisation parameter.. Write the filters appropriately... careful: do not divide by zero! Compute the filtering, reshape, use ifft2 to compute the output deblurred image. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 13 / 17
Tikhonov filtering Idea: correct Φ by imposing that the denominator never vanishes. φ i = σ2 i σ 2 i + λ, λ > 0, where λ is the Tikhonov regularisation parameter. Task: write Tikhonov filtering function. Write function tik fft2 with the same input as before with additional λ=0.01 regularisation parameter.. Write the filters appropriately... careful: do not divide by zero! Compute the filtering, reshape, use ifft2 to compute the output deblurred image. + Variations: noise? Choice of λ? Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 13 / 17
The choice of λ Tikhonov functional: Heuristically: min y Bx 2 2 + λ x 2 small λ: low regularisation, high data-fitting. Reconstruction is still noisy since oscillations are preserved. large λ: high regularisation, low data-fitting. Reconstruction is over-smoothed. How to choose the optimal λ? The optimal λ is somewhere in between. Is there an automatic method to choose it? Which prior information do we need? Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 14 / 17
The choice of λ Tikhonov functional: Heuristically: min y Bx 2 2 + λ x 2 small λ: low regularisation, high data-fitting. Reconstruction is still noisy since oscillations are preserved. large λ: high regularisation, low data-fitting. Reconstruction is over-smoothed. How to choose the optimal λ? The optimal λ is somewhere in between. Is there an automatic method to choose it? Which prior information do we need? prior information on the noise level σ 2 : discrepancy principle. no prior knowledge on the noise level: Generalised Cross Validation (GCV). Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 14 / 17
Outline 1 Exploiting PSF structure for BCCB matrix computation 2 TSVD and Tikhonov implementation 3 Tikhonov parameter choice via GCV Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 15 / 17
GCV for Tikhonov filtering GCV functional for general filter matrix Φ: G(λ) = (Id Φ)ŷ 2 (tr(id Φ)) 2, so its form depends on the filter used. Note that this does not make sense when Φ = Id (non-filtered case). Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 16 / 17
GCV for Tikhonov filtering GCV functional for Tikhonov filter matrix Φ: G(λ) = N i=1 ( ŷ σ 2 i +λ ) 2 ( N i=1 ) 2 1 σi 2+λ It can be shown that the minimum of the functional G is unique and it corresponds to the optimal choice of λ (leave-one-out principe). Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 16 / 17
GCV for Tikhonov filtering GCV functional for Tikhonov filter matrix Φ: G(λ) = N i=1 ( ŷ σ 2 i +λ ) 2 ( N i=1 ) 2 1 σi 2+λ It can be shown that the minimum of the functional G is unique and it corresponds to the optimal choice of λ (leave-one-out principe). Task: implement GCV function for Tikohonov filter. In particular: Input: PSF, blurred image. Output: optimal λ. Write the GCV functional as a @ GCV (function handle). Input? use fminbound MATLAB inbuilt function to get the minimum of GCV functional. Use box constraints to bound the solution: λ=fminbnd(@ GCV, min(abs(s)), max(abs(s)), [], s, bhat); Show result with optimal λ. Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 16 / 17
References Inspiration and references are taken from: - P. C. Hansen, J. G. Nagy, D. P. O Leary, Deblurring Images, Matrices, Spectra and Filtering, SIAM, Philadelphia, 2006. - Online material and inspiring MATLAB toolboxes: http://www.imm.dtu.dk/~pcha/hno/ - Slides on blur matrices: http: //www.mathcs.emory.edu/~nagy/courses/fall06/id_lecture1.pdf Luca Calatroni (DIMA, Unige) Esercitazione 2, Lab. Prob. Inv. Aprile 2016 17 / 17