DWT introduction: part II

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 bakery256x256.ppm.

1D DWT basis functions for incomplete decomposition (e.g. 1-level)

Haar wavelets

Haar low-pass filter

haar = HaarFilter[]

Haar high-pass filter

HighPassFilter[haar]

Example Haar-wavelet basis functions for 1-level DWT

zeros  =  Table[0, {16}];

Table[
        ListPlot[InverseWavelet1D[ReplacePart[zeros, 1, i], haar, 1], 
          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)

Set image directory

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

2D DWT detailed "compression" example #1

Load gray-scale image

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

Display image

ShowImage[img, scale];

Define wavelet filter

db4 = DaubechiesFilter[4];

Compression example (Daubechies-4, 5-level)

DWT output size

imgsize = 500;

Define filter, levels,  output options

filter = db4;
level = 5;
func = Abs;
plotstyle = PlotRange -> Automatic;

DWT

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

Eliminate progressively more small wavelet coefficients

CompressImage[dwt_, thresh_] := 
        Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

dwtc = CompressImage[dwt, #] &  /@  {1, 5, 10, 25, 50, 75, 100, 
            150};

Count number of nonzero wavelet coefficients as threshold is increased

(100 ((Times  @@  
                        Dimensions[img]) - (Position[#, 0]  //  
                        Length))/(Times  @@  
                    Dimensions[img])) &  /@  dwtc  //  N

Show compressed DWTs

ShowWaveletTransform2D[#, level, func,  plotstyle, 
            ImageSize -> imgsize] &  /@  dwtc;

Take inverse DWTs

imgc = InverseWaveletTransform2D[#, filter, level] &  /@  
          dwtc;

Show compressed images

ShowImage[#, scale] &  /@  imgc;

2D DWT detailed  "compression" example #2

Load gray-scale image

img = 
        ReadImage["bakery256x256.ppm"];
scale  =  1;

Display image

ShowImage[img, scale];

Define wavelet filter

db4 = DaubechiesFilter[4];

Compression example (Daubechies-4, 5-level)

DWT output size

imgsize = 500;

Define filter, levels, output options

filter = db4;
level = 5;
func = Abs;
plotstyle = PlotRange -> Automatic;

DWT

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

Eliminate progressively more small wavelet coefficients

CompressImage[dwt_, thresh_] := 
        Map[If[Abs[#]  <  thresh, 0, #] &, dwt, {2}];

dwtc = CompressImage[dwt, #] &  /@  {1, 5, 10, 25, 50, 75, 100, 
            150};

Count number of nonzero wavelet coefficients as threshold is increased

(100 ((Times  @@  
                        Dimensions[img]) - (Position[#, 0]  //  
                        Length))/(Times  @@  
                    Dimensions[img])) &  /@  dwtc  //  N

Show compressed DWTs

ShowWaveletTransform2D[#, level, func,  plotstyle, 
            ImageSize -> imgsize] &  /@  dwtc;

Take inverse DWTs

imgc = InverseWaveletTransform2D[#, filter, level] &  /@  
          dwtc;

Show compressed images

ShowImage[#, scale] &  /@  imgc;