Sie sind auf Seite 1von 10

2-D Haar Wavelets

This numerical tour explores 2-D multiresolution analysis with the Haar transform. It
was introduced in 1910 by Haar [Haar1910] and is arguably the first example of
wavelet basis.

Contents
Installing toolboxes and setting up the path.
Forward 2-D Haar transform
Inverse 2-D Haar transform.
Haar Linear Approximation
Haar Non-linear Approximation
Bibliography

Installing toolboxes and setting up the path.


You need to download the following files: signal toolbox and general toolbox.

You need to unzip these toolboxes in your working directory, so that you have
toolbox_signal and toolbox_general in your directory.

For Scilab user: you must replace the Matlab comment '%' by its Scilab counterpart
'//'.

Recommandation: You should create a text file named for instance numericaltour.sce
(in Scilab) or numericaltour.m (in Matlab) to write all the Scilab/Matlab command you
want to execute. Then, simply run exec('numericaltour.sce'); (in Scilab) or numericaltour;
(in Matlab) to run the commands.

Execute this line only if you are using Matlab.

getd = @(p)path(p,path); % scilab users must *not* execute this

Then you can add the toolboxes to the path.

getd('toolbox_signal/');
getd('toolbox_general/');

Forward 2-D Haar transform


The Haar transform is the simplest orthogonal wavelet transform. It is computed by
iterating difference and averaging between odd and even samples of the signal. Since
we are in 2-D, we need to compute the average and difference in the horizontal and
then in the vertical direction (or in the reverse order, it does not mind).
Size of the image.
N =nn
n = 256;
N = n*n;

First we load an image.

name = 'hibiscus';
f = load_image(name,n);
f = rescale( sum(f,3) );

The Haar transform operates over J = log2 (n) 1 scales. It computes a series of
n n
coarse scale and fine scale coefficients aj , djH , djV , djD R j j where Nj = 2 .
j

J = log2(n)-1;

The forward Haar transform computes


,D
H(f) = (dj )=H,V
j=0,,J {a0 }.

Note that the set of coarse scale coefficients (aj )j>0 are not stored.

This transform is orthogonal, meaning H H = Id , and that there is the following


conservation of energy

|fi |2 = ||f||2 = ||Hf||2 = ||dj ||2 + |a0 |2 .


i j,

One initilizes the algorithm with aJ+1 = f .

The first step apply a vertical transformtion, which corresponds to applying a 1-D Haar
~ ~
transform on each column, i.e. it computes d j , a j from aj+1 as, for all
= 0, , 2j+1 1 and k = 0, , 2j 1 ,

~ [k, ] = aj+1 [2k, ] + aj+1 [2k + 1, ]


a and
~
d j [k, ] =
aj+1 [2k, ] aj+1 [2k + 1, ]
.
j
2 2

Shortcut to compute a~ , d~ from a


j j j+1 .

haarV = @(a)[ a(1:2:length(a),:) + a(2:2:length(a),:);


a(1:2:length(a),:) - a(2:2:length(a),:) ]/sqrt(2);

Display the result of the vertical transform.

clf;
imageplot(f,'Original image',1,2,1);
imageplot(haarV(f),'Vertical transform',1,2,2);
One then apply the same 1-D horizontal transform to both ~ , d~ to obtain
a the
j j
resulting matrices at scale j , i.e. to compute aj , dj , dj , djD as, for
H V all
j
, k = 0, ,2 1 ,
~ [k, 2] + a
a ~ [k, 2 + 1] ~ [k, 2] a
a ~ [k, 2 + 1]
j j j j
aj [k, ] = and djH [k, ] = .
2 2
~ ~ ~ ~
d j [k, 2] + d j [k, 2 + 1] d j [k, 2] d j [k, 2 + 1]
dj [k, ] =
V
and djD [k, ] = .
2 2
Shortcut for the vertical transform.

haarH = @(a)haarV(a')';

Shortcut for one step of Haar transform.

haar = @(a)haarH(haarV(a));

Display the result of the first step of the algorithm.

clf;
imageplot(f,'Original image',1,2,1);
subplot(1,2,2);
plot_wavelet(haar(f),log2(n)-1); title('Transformed');
The output of the forward transform is stored in the variable fw. At a given iteration
indexed by a scale , it will store in fw(1:2^(j+1),1:2^(j+1)) the variable aj+1 .

Initializes the algorithm at scale j=J .

j = J;

Initialize fw to the full signal.

fw = f;

At iteration indexed by j, select the sub-part of the signal containing aj+1 , and apply
it the Haar operator.

fw(1:2^(j+1),1:2^(j+1)) = haar(fw(1:2^(j+1),1:2^(j+1)));

Exercice 1: (check the solution) Implement a full wavelet transform that extract
iteratively wavelet coefficients, by repeating these steps. Take care of choosing the
correct number of steps.

exo1;
Check that the transform is orthogonal, which means that the energy of the coefficient
is the same as the energy of the signal.

fprintf('Should be 0: %.3f.\n', (norm(f(:))-norm(fw(:)))/norm(f(:)));

Should be 0: 0.000.

Display the wavelet coefficients.

clf;
subplot(1,2,1);
imageplot(f); title('Original');
subplot(1,2,2);
plot_wavelet(fw, 1); title('Transformed');
Inverse 2-D Haar transform.
The backward transform computes a signal f1 = H (h) from a set of coefficients
h= (dj )j, {a0 }

If h = H(f) are the coefficients of an image f , one recovers f1 =f .

The inverse transform starts from j=0 , and computes aj+1 from the knowledge of
aj , dj by first inverting the horizontal transform, for all k, = 0, , 2j 1 ,

aj [k, ] + aH
j [k, ] aj [k, ] aH
j [k, ]
~ [k, 2] =
a and ~ [k, 2 + 1] =
a
j j
2 2

~ djV [k, ] + djD [k, ] ~ djV [k, ] djD [k, ]


d j [k, 2] = and d j [k, 2 + 1] = .
2 2

ihaarV = @(a,d)assign( zeros(2*size(a,1),size(a,2)), ...


[1:2:2*size(a,1), 2:2:2*size(a,1)], [a+d; a-d]/sqrt(2), 1 );

Then one invert the vertical transform, for all k = 0, , 2j 1 and for all
j+1
= 0, ,2 1
~ [k, ] + d~ [k, ]
a ~ [k, ] d~ [k, ]
a
j j j j
aj [2k, ] = and aj [2k + 1, ] =
2 2

ihaarH = @(a,d)ihaarV(a',d')';

Shortcut to invert horizontal and then vertical transforms. This defines the invert of the
Haar transform.

ihaar = @(a,dH,dV,dD)ihaarV( ihaarH(a,dH), ihaarH(dV,dD) );

Initialize the signal to recover f1 as the transformed coefficient, and select the
smallest possible scale j = 0 .

f1 = fw;
j = 0;

Apply one step of inverse transform.

s = 1:2^j; t = 2^j+1:2^(j+1); u = 1:2^(j+1);


f1(u,u) = ihaar(f1(s,s),f1(s,t),f1(t,s),f1(t,t));

Exercice 2: (check the solution) Write the inverse wavelet transform that computes f1
from coefficients fW.
exo2;

Check that we recover exactly the original image.

fprintf('Should be 0: %.3f.\n', (norm(f-f1))/norm(f));

Should be 0: 0.000.

Haar Linear Approximation


Linear approximation is obtained by setting to zeros the Haar coefficient coefficients
above a certain scale j .

Cut-off scale.

j = J-1;

Set to zero fine scale coefficients.

fw1 = zeros(n); fw1(1:2^j,1:2^j) = fw(1:2^j,1:2^j);

Computing the signal f1 corresponding to these coefficients fw1 computes the


orthogonal projection on the space of images that are constant on disjoint squares of
j j
length N/2 N/2 .
Exercice 3: (check the solution) Display the reconstructed signal obtained from fw1, for
a decreasing cut-off scale j .

exo3;

Haar Non-linear Approximation


Non-linear approximation is obtained by thresholding low amplitude wavelet
coefficients. It is defined as

fT = H ST H(f)
where ST is the hard tresholding operator

xi if |xi | > T ,
ST (x)i = { .
0 otherwise.

S = @(x,T) x .* (abs(x)>T);

Set the threshold value.

T = .1;

Threshold the coefficients.

fwT = S(fw,T);
Display the coefficients before and after thresholding.

clf;
subplot(1,2,1);
plot_wavelet(fw); axis('tight'); title('Original coefficients');
subplot(1,2,2);
plot_wavelet(fwT); axis('tight'); title('Thresholded coefficients');

Exercice 4: (check the solution) Find the threshold T so that the number of remaining
coefficients in fwT is a fixed number m . Use this threshold to compute fwT and then
display the corresponding approximation f1 of f . Try for an increasing number m of
coeffiients.

exo4;
Bibliography
[Haar1910] Haar A. Zur Theorie der orthogonalen Funktionensysteme,
Mathematische Annalen, 69, pp 331-371, 1910.

Copyright (c) 2010 Gabriel Peyre

Das könnte Ihnen auch gefallen