# Image Approximation with Fourier and Wavelets¶


This numerical tour overviews the use of Fourier and wavelets for image approximation.

In [2]:
addpath('toolbox_signal')


Note: to measure the error of an image $f$ with its approximation $f_M$, we use the SNR measure, defined as $$\text{SNR}(f,f_M) = -20\log_{10} \pa{ \frac{ \norm{f-f_M} }{ \norm{f} } },$$ which is a quantity expressed in decibels (dB). The higer the SNR, the better the quality.

First we load an image $f \in \RR^N$ of $N = N_0 \times N_0$ pixels.

In [3]:
name = 'lena';
n0 = 512;


Display the original image.

In [4]:
clf;
imageplot( f, 'Image f');


Display a zoom in the middle.

In [5]:
clf;
imageplot( crop(f,64), 'Zoom' );


An image is a 2D array, that can be modified as a matrix.

In [6]:
clf;
imageplot(-f, '-f', 1,2,1);
imageplot(f(n0:-1:1,:), 'Flipped', 1,2,2);


Blurring is achieved by computing a convolution $f \star h$ with a kernel $h$.

Compute the low pass kernel.

In [7]:
k = 9; % size of the kernel
h = ones(k,k);
h = h/sum(h(:)); % normalize


Compute the convolution $f \star h$.

In [8]:
fh = perform_convolution(f,h);


Display.

In [9]:
clf;
imageplot(fh, 'Blurred image');


## Fourier Transform¶

The Fourier orthonormal basis is defined as $$\psi_m(k) = \frac{1}{\sqrt{N}}e^{\frac{2i\pi}{N_0} \dotp{m}{k} }$$ where $0 \leq k_1,k_2 < N_0$ are position indexes, and $0 \leq m_1,m_2 < N_0$ are frequency indexes.

The Fourier transform $\hat f$ is the projection of the image on this Fourier basis $$\hat f(m) = \dotp{f}{\psi_m}.$$

The Fourier transform is computed in $O(N \log(N))$ operation using the FFT algorithm (Fast Fourier Transform). Note the normalization by $\sqrt{N}=N_0$ to make the transform orthonormal.

In [10]:
F = fft2(f) / n0;


We check this conservation of the energy.

In [11]:
disp(strcat(['Energy of Image:   ' num2str(norm(f(:)))]));
disp(strcat(['Energy of Fourier: ' num2str(norm(F(:)))]));

Energy of Image:   255.9831
Energy of Fourier: 255.9831


Compute the logarithm of the Fourier magnitude $\log(\abs{\hat f(m)} + \epsilon)$, for some small $\epsilon$.

In [12]:
L = fftshift(log( abs(F)+1e-1 ));


Display. Note that we use the function |fftshift| is useful to put the 0 low frequency in the middle.

In [13]:
clf;
imageplot(L, 'Log(Fourier transform)');


## Linear Fourier Approximation¶

An approximation is obtained by retaining a certain set of index $I_M$ $$f_M = \sum_{ m \in I_M } \dotp{f}{\psi_m} \psi_m.$$

Linear approximation is obtained by retaining a fixed set $I_M$ of $M = \abs{I_M}$ coefficients. The important point is that $I_M$ does not depend on the image $f$ to be approximated.

For the Fourier transform, a low pass linear approximation is obtained by keeping only the frequencies within a square. $$I_M = \enscond{m=(m_1,m_2)}{ -q/2 \leq m_1,m_2 < q/2 }$$ where $q = \sqrt{M}$.

This can be achieved by computing the Fourier transform, setting to zero the $N-M$ coefficients outside the square $I_M$ and then inverting the Fourier transform.

Number $M$ of kept coefficients.

In [14]:
M = n0^2/64;


Exercise 1

Perform the linear Fourier approximation with $M$ coefficients. Store the result in the variable |fM|. isplay

In [15]:
exo1()

In [16]:
%% Insert your code here.


Compare two 1D profile (lines of the image). This shows the strong ringing artifact of the linea approximation.

In [17]:
clf;
subplot(2,1,1);
plot(f(:,n0/2));
axis('tight'); title('f');
subplot(2,1,2);
plot(fM(:,n0/2));
axis('tight'); title('f_M');


## Non-linear Fourier Approximation¶

Non-linear approximation is obtained by keeping the $M$ largest coefficients. This is equivalently computed using a thresholding of the coefficients $$I_M = \enscond{m}{ \abs{\dotp{f}{\psi_m}}>T }.$$

Set a threshold $T>0$.

In [18]:
T = .2;


Compute the Fourier transform.

In [19]:
F = fft2(f) / n0;


Do the hard thresholding.

In [20]:
FT = F .* (abs(F)>T);


Display. Note that we use the function |fftshift| is useful to put the 0 low frequency in the middle.

In [21]:
clf;
L = fftshift(log( abs(FT)+1e-1 ));
imageplot(L, 'thresholded Log(Fourier transform)');


Inverse Fourier transform to obtained $f_M$

In [22]:
fM = real( ifft2(FT)*n0 );


Display.

In [23]:
clf;
imageplot(clamp(fM), ['Non-linear, Fourier, SNR=' num2str(snr(f,fM), 4) 'dB']);


Given a $T$, the number of coefficients is obtained by counting the non thresholded coefficients $\abs{I_M}$.

In [24]:
m = sum(FT(:)~=0);
disp(['M/N = 1/'  num2str(round(n0^2/m)) '.']);

M/N = 1/32.


Exercise 2

Compute the value of the threshold $T$ so that the number of coefficients is $M$. Display the corresponding approximation $f_M$. isplay

In [25]:
exo2()