Discrete Wavelet Transform (DWT) introduction

Note: This notebook assumes that all definitions in ipcv_definitions.nb have been evaluated first. It also assumes that the directory in the Set image directory points to an image directory with the following images: cat256x256.ppm and part128x128.ppm.

1D DWT basis function examples

Haar wavelets

Haar low-pass filter

haar = HaarFilter[]

Haar high-pass filter

HighPassFilter[haar]

Example Haar-wavelet basis functions

zeros  =  Table[0, {128}];

Table[
        ListPlot[InverseWavelet1D[ReplacePart[zeros, 1, i], haar], 
          PlotRange -> All, PlotJoined -> True, Frame -> True, 
          PlotStyle -> {Red, Thickness[ .005]}, 
          ImageSize -> 288], {i, 1, Length[zeros]}];

(Web: click on image to see all 64 basis functions)

Daubechies-4 wavelets

Daubechies low-pass filter

db4 = DaubechiesFilter[4]

Daubechies high-pass filter

HighPassFilter[db4]

Example Daubechies-wavelet basis functions

zeros  =  Table[0, {128}];

Table[
        ListPlot[InverseWavelet1D[ReplacePart[zeros, 1, i], db4], 
          PlotRange -> All, PlotJoined -> True, Frame -> True, 
          PlotStyle -> {Red, Thickness[ .005]}, 
          ImageSize -> 288], {i, 1, Length[zeros]}];

(Web: click on image to see all 64 basis functions)

Daubechies-8 wavelets

Daubechies low-pass filter

db8 = DaubechiesFilter[8]

Daubechies high-pass filter

HighPassFilter[db8]

Example Daubechies-wavelet basis functions

zeros  =  Table[0, {128}];

Table[
        ListPlot[InverseWavelet1D[ReplacePart[zeros, 1, i], db8], 
          PlotRange -> All, PlotJoined -> True, Frame -> True, 
          PlotStyle -> {Red, Thickness[ .005]}, 
          ImageSize -> 288], {i, 1, Length[zeros]}];

(Web: click on image to see all 64 basis functions)

1D DWT decomposition visualization

1-, 2-, 3-, 4- and 5-level DWT decomposition (single color = one frequency level; frequencies from left to right vary from lowest to highest; locations from left to right vary from left to right in original image).

VisualizeBands1D[#, ImageSize -> 600] &  /@  
        Table[i, {i, 1, 5}];

2D DWT decomposition visualization

Level 1

ShowPyramid2D[1,  ImageSize -> 400];

Level 2

ShowPyramid2D[2,  ImageSize -> 400];

Level 3

ShowPyramid2D[3,  ImageSize -> 400];

Level 4

ShowPyramid2D[4,  ImageSize -> 400];

Level 5

ShowPyramid2D[5,  ImageSize -> 400];

2D DWT basis function examples

Haar example

haar = HaarFilter[]

dim = 64;
which = {6, 6};

tmp = 
        InverseWaveletTransform2D[
          ReplacePart[Table[0, {dim}, {dim}], 1, which], haar, 
          7];
ListPlot3D[tmp,  Mesh -> False, PlotRange -> All, 
        ImageSize -> 500];

Daubechies-4 example

db4 = DaubechiesFilter[4]

dim = 64;
which = {6, 6};

tmp = 
        InverseWaveletTransform2D[
          ReplacePart[Table[0, {dim}, {dim}], 1, which], db4, 7];
ListPlot3D[tmp, Mesh -> False,  PlotRange -> All, 
        ImageSize -> 500];

Daubechies-8 example

db8 = DaubechiesFilter[8]

dim = 64;
which = {7, 7};

tmp = 
        InverseWaveletTransform2D[
          ReplacePart[Table[0, {dim}, {dim}], 1, which], db8, 7];
ListPlot3D[tmp, Mesh -> False,  PlotRange -> All, 
        ImageSize -> 500];

Set image directory

If[StringQ[$ImagePath], $path = $ImagePath,  $path = "."]; 
    SetDirectory[$path];

2D DWT image example #1

Load gray-scale image

img = ReadImage["cat256x256.ppm"];
scale  =  1.5;

Display image

ShowImage[img, scale];

Define wavelet filters

haar = HaarFilter[];
db4 = DaubechiesFilter[4];
db8 = DaubechiesFilter[8];

One-level DWT (signed, full plot range)

DWT output size

imgsize = 500;

Define level

level = 1;
func = Signed;
plotstyle = PlotRange -> All;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

One-level DWT (signed, more detail)

DWT output size

imgsize = 500;

Define level

level = 1;
func = Signed;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

One-level DWT (magnitude, more detail)

DWT output size

imgsize = 500;

Define level

level = 1;
func = Abs;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Two-level DWT (magnitude, more detail)

DWT output size

imgsize = 500;

Define level

level = 2;
func = Abs;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Three-level DWT (magnitude, more detail)

DWT output size

imgsize = 500;

Define level

level = 3;
func = Abs;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Image compression example #1 (Haar, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, threshold, output options

filter = haar;
level = 5;
thresh = 50. ;
func = Abs;
plotstyle = PlotRange -> All;

5-level DWT

dwt = WaveletTransform2D[img, filter, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Eliminate small wavelet coefficients

dwtc = Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

Count number of nonzero wavelet coefficients

(Times  @@  Dimensions[img]) - (Position[dwtc, 0]  //  
          Length)

Show compressed DWT

ShowWaveletTransform2D[dwtc, level, func,  plotstyle, 
        ImageSize -> imgsize];

Take inverse DWT

imgc = InverseWaveletTransform2D[dwtc, filter, level];

Show compressed image

ShowImage[imgc, scale];

Image compression example #2 (Daubechies-4, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, threshold, output options

filter = db4;
level = 5;
thresh = 50. ;
func = Abs;
plotstyle = PlotRange -> All;

DWT

dwt = WaveletTransform2D[img, filter, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Eliminate small wavelet coefficients

dwtc = Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

Count number of nonzero wavelet coefficients

(Times  @@  Dimensions[img]) - (Position[dwtc, 0]  //  
          Length)

Show compressed DWT

ShowWaveletTransform2D[dwtc, level, func,  plotstyle, 
        ImageSize -> imgsize];

Take inverse DWT

imgc = InverseWaveletTransform2D[dwtc, filter, level];

Show compressed image

ShowImage[imgc, scale];

Image compression example #2 (Daubechies-8, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, threshold, output options

filter = db8;
level = 5;
thresh = 50. ;
func = Abs;
plotstyle = PlotRange -> All;

DWT

dwt = WaveletTransform2D[img, filter, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Eliminate small wavelet coefficients

dwtc = Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

Count number of nonzero wavelet coefficients

(Times  @@  Dimensions[img]) - (Position[dwtc, 0]  //  
          Length)

Show compressed DWT

ShowWaveletTransform2D[dwtc, level, func,  plotstyle, 
        ImageSize -> imgsize];

Take inverse DWT

imgc = InverseWaveletTransform2D[dwtc, filter, level];

Show compressed image

ShowImage[imgc, scale];

2D DWT image example #2

Load gray-scale image

img = 
        ColorToGrayScale[
          ReadImage["part128x128.ppm"]];
scale  =  3;

Display image

ShowImage[img, scale];

Define wavelet filters

haar = HaarFilter[];
db4 = DaubechiesFilter[4];
db8 = DaubechiesFilter[8];

One-level DWT (signed, full plot range)

DWT output size

imgsize = 500;

Define level

level = 1;
func = Signed;
plotstyle = PlotRange -> All;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

One-level DWT (signed, more detail)

DWT output size

imgsize = 500;

Define level

level = 1;
func = Signed;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

One-level DWT (magnitude, more detail)

DWT output size

imgsize = 500;

Define level

level = 1;
func = Abs;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Two-level DWT (magnitude, more detail)

DWT output size

imgsize = 500;

Define level

level = 2;
func = Abs;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Three-level DWT (magnitude, more detail)

DWT output size

imgsize = 500;

Define level

level = 3;
func = Abs;
plotstyle = PlotRange -> Automatic;

Haar

dwt = WaveletTransform2D[img, haar, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-4

dwt = WaveletTransform2D[img, db4, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Debauchies-8

dwt = WaveletTransform2D[img, db8, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Image compression example #1 (Haar, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, threshold, output options

filter = haar;
level = 5;
thresh = 50. ;
func = Abs;
plotstyle = PlotRange -> All;

5-level DWT

dwt = WaveletTransform2D[img, filter, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Eliminate small wavelet coefficients

dwtc = Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

Count number of nonzero wavelet coefficients

(Times  @@  Dimensions[img]) - (Position[dwtc, 0]  //  
          Length)

Show compressed DWT

ShowWaveletTransform2D[dwtc, level, func,  plotstyle, 
        ImageSize -> imgsize];

Take inverse DWT

imgc = InverseWaveletTransform2D[dwtc, filter, level];

Show compressed image

ShowImage[imgc, scale];

Image compression example #2 (Daubechies-4, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, threshold, output options

filter = db4;
level = 5;
thresh = 50. ;
func = Abs;
plotstyle = PlotRange -> All;

DWT

dwt = WaveletTransform2D[img, filter, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Eliminate small wavelet coefficients

dwtc = Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

Count number of nonzero wavelet coefficients

(Times  @@  Dimensions[img]) - (Position[dwtc, 0]  //  
          Length)

Show compressed DWT

ShowWaveletTransform2D[dwtc, level, func,  plotstyle, 
        ImageSize -> imgsize];

Take inverse DWT

imgc = InverseWaveletTransform2D[dwtc, filter, level];

Show compressed image

ShowImage[imgc, scale];

Image compression example #2 (Daubechies-8, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, threshold, output options

filter = db8;
level = 5;
thresh = 50. ;
func = Abs;
plotstyle = PlotRange -> All;

DWT

dwt = WaveletTransform2D[img, filter, level]; 
    ShowWaveletTransform2D[dwt, level, func,  plotstyle, 
      ImageSize -> imgsize];

Eliminate small wavelet coefficients

dwtc = Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

Count number of nonzero wavelet coefficients

(Times  @@  Dimensions[img]) - (Position[dwtc, 0]  //  
          Length)

Show compressed DWT

ShowWaveletTransform2D[dwtc, level, func,  plotstyle, 
        ImageSize -> imgsize];

Take inverse DWT

imgc = InverseWaveletTransform2D[dwtc, filter, level];

Show compressed image

ShowImage[imgc, scale];