% This function compares the results of linear and nonlinear
% wavelet filtering performed on Lenna

% Note that this code requires the toolbox 'wavelab' to be available in MATLAB 
% (run the script "WavePath.m" to perform all initializations)

function wavelet_06

echo off, clc
clear all

%close all;

colormap('gray');
I =  ReadImage('Lenna');
figure(1); imagesc(I);

%qmf = MakeONFilter('Haar');
%qmf = MakeONFilter('Daubechies',6);
qmf = MakeONFilter('Symmlet',4);
%qmf = MakeONFilter('Coiflet',4);

%M = 4^7;
M = 10 * 128;

approx2dlin(I,M,qmf,2);
approx2dnonlin(I,M,qmf,3);


function res = approx2dlin(I,M,qmf,Fig)
  m = ceil(sqrt(M));
  N = size(I,1);
  wc = FWT2_PO(I,2,qmf);
  for (i=(m+1):N)
    wc(:,i) = zeros(N,1);
    wc(i,:) = zeros(1,N);
  end;
  res = IWT2_PO(wc,2,qmf);
  figure(Fig); colormap('gray'); imagesc(res);

  
  
function res = approx2dnonlin(I,M,qmf,Fig)
  N = size(I,1);
  wc = FWT2_PO(I,2,qmf);
  trie = zeros(1,N*N);
  for (i=1:N)
    trie(1,((i-1)*N+1):i*N) = wc(i,:);
    trie=abs(trie);
  end;
  trie = sort(trie);
  seuil = trie(N*N-M);
  wc = wc .* (abs(wc) > seuil);
  res = IWT2_PO(wc,2,qmf);
  figure(Fig); colormap('gray'); imagesc(res);
  
