# Wavelet Block Thresholding¶


This numerical tour presents block thresholding methods, that makes use of the structure of wavelet coefficients of natural images to perform denoising. Theoretical properties of block thresholding were investigated in CaiSilv Cai99 HallKerkPic99

In [2]:
warning off
warning on


## Generating a Noisy Image¶

Here we use an additive Gaussian noise.

Size of the image of $N=n \times n$ pixels.

In [3]:
n = 256;


First we load an image $f_0 \in \RR^N$.

In [4]:
name = 'boat';


Display it.

In [5]:
clf; imageplot(f0);


Noise level.

In [6]:
sigma = .08;


Generate a noisy image $f=f_0+\epsilon$ where $\epsilon \sim \Nn(0,\si^2\text{Id}_N)$.

In [7]:
f = f0 + sigma*randn(size(f0));


Display it.

In [8]:
clf; imageplot(clamp(f));


## Orthogonal Wavelet Thresholding¶

We first consider the traditional wavelet thresholding method.

Parameters for the orthogonal wavelet transform.

In [9]:
Jmin = 4;
options.ti = 0;


Shortcuts for the foward and backward wavelet transforms.

In [10]:
wav  = @(f)perform_wavelet_transf(f,Jmin,+1,options);
iwav = @(fw)perform_wavelet_transf(fw,Jmin,-1,options);


Display the original set of noisy coefficients.

In [11]:
clf;
plot_wavelet(wav(f),Jmin);


Denoting $\Ww$ and $\Ww^*$ the forward and backward wavelet transform, wavelet thresholding $\tilde f$ is defined as $$\tilde f = \Ww^* \circ \theta_T \circ \Ww(f)$$ where $T>0$ is the threshold, that should be adapted to the noise level.

The thresholding operator is applied component-wise $$\th_T(x)_i = \psi_T(x_i) x_i$$ where $\psi_T$ is an atenuation fonction. In this tour, we use the James Stein (JS) attenuation: $$\psi_T(s) = \max\pa{ 0, 1-\frac{T^2}{s^2} }$$

In [12]:
psi= @(s,T)max3(1-T^2 ./ max(abs(s).^2,1e-9),0);


Display the thresholding function $\th_T$.

In [13]:
t = linspace(-3,3,1024);
clf; hold on;
plot(t,t.*psi(t,1));
plot(t,t, 'r--'); axis equal;


Thresholding operator.

In [14]:
theta = @(x,T)psi(x,T).*x;
ThreshWav = @(f,T)iwav(theta(wav(f),T));


Test the thresholding.

In [15]:
T = 1.5*sigma;
clf;
imageplot(clamp( ThreshWav(f,T) ));


Exercise 1

Display the evolution of the denoising SNR when $T$ varies. Store in |fThresh| the optimal denoising result. etrieve best

In [16]:
exo1()

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


Display the optimal thresolding.

In [18]:
clf;
imageplot(clamp(fThresh), strcat(['SNR=' num2str(snr(f0,fThresh),3)]));


## Block Thresholding Operator¶

A block thresholding operator of coefficients $x=(x_i)_{i=1}^P \in \RR^P$ is defined using a dijoint partition into a set of blocks $B$ $$\{1,\ldots,P\} = \bigcup_{b \in B} b.$$ Its definition reads $$\forall i \in b, \quad \theta_T(x)_i = \psi_T( \norm{x_b}_2 ) x_i$$ where $x_b = (x_j)_{j \in B} \in \RR^{\abs{b}}$. One thus thresholds the $\ell^2$ norm (the energy) of each block rather than each coefficient independently.

For image-based thresholding, we use a partition in square blocks of equal size $w \times w$.

The block size $w$.

In [19]:
w = 4;


Compute indexing of the blocks.

In [20]:
[dX,dY,X,Y] = ndgrid(0:w-1,0:w-1,1:w:n-w+1,1:w:n-w+1);
I = X+dX + (Y+dY-1)*n;


Block extraction operator. It returns the set of $\{x_b\}_{b \in B}$ of block-partitioned coefficients.

In [21]:
block = @(x)reshape(x(I(:)),size(I));


Block reconstruction operator.

In [22]:
iblock = @(H)assign(zeros(n), I, H);


Check that block extraction / reconstruction gives perfect reconstruction.

In [23]:
mynorm = @(x)norm(x(:));
fprintf('Should be 0: %.3f\n', mynorm(f - iblock(block(f))) );

Should be 0: 0.000


Compute the average energy of each block, and duplicate.

In [24]:
repm = @(v)repmat( max3(v,1e-15), [w w]);
energy = @(H)repm( sqrt( mean(mean(abs(H).^2,1),2) ) );


Block thresholding operator.

In [25]:
Thresh = @(H,T)psi(energy(H),T).*H;
ThreshBlock = @(x,T)iblock( Thresh(block(x),T) );


Exercise 2

Test the effect of block thresholding on the image $f_0$ itself, for increasing value of $T$. (of course thresholding directly the image has no interest, this is just to vizualize the effect).

In [26]:
exo2()

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


## Orthogonal Wavelet Block Thresholding¶

Wavelet coefficients of natural images are not independant one from each other. One can thus improve the denoising results by thresholding block of coefficients togethers. Block thresholding is only efficient when used as a soft thresholder. Here we use a Stein soft thresholder.

Display the thresholded coefficients for a threshold value $T$ proportional to the noise level $\si$.

In [28]:
T = 1.25*sigma;
clf;
plot_wavelet( ThreshBlock(wav(f),T), Jmin);