% routine to show noise removal by redundant wavelet transform (RWT)

clear
% select the input imag

what_image = input('input = (0) knee or (1) house or (2) Lena? ');
if isempty(what_image), what_image = 0; end
   switch what_image
   case 1
      disp('read house')
      house = imread('house.tif','tif');
      house = double(house); % 256 x 256
      clean = house;
   case 2
      disp('read Lenna')
      lenna = ReadImage('Lenna'); %WaveLab
      lenna = double(lenna); %256 x 256
      clean = lenna;
   otherwise
      disp('read knee')
      knee = imread('knee.tif','tif');
      knee = double(knee); %128 x 128
      clean = knee;
   end

% select the noise to be added

what_noise = input('What noise? (0) none, (1) white, (2) colored or (3) noise matrix? ');
if isempty(what_noise), what_noise = 1; end
switch what_noise
 case 0 % no (extra) noise
   disp('No extra noise')
   noise = zeros(size(clean));
 case 1 % white noise
   disp('White noise')
   whitenoise = randn(size(clean));
   noise = whitenoise*5;
 case 2 % colored noise
   disp('Color white noise')
   whitenoise = randn(size(clean))*5;
   % make 2D highpass-filter for noise coloring
   % fir1: 1D filter-design ( from signal processing toolbox)
   % HP6 = fir1(100,0.6,'high');
   % save fir1 HP6 -APPEND; % if signal toolbox not available
   % load fir1 HP6; % then make filter and read it
   % HP6 = ...
   %  [-1.249952937465984e-18  3.455252881204604e-03 -3.933584849155312e-03 ...
   %  -7.221104373597301e-03 2.011451899152559e-02 8.437182327895390e-18 ...
   %  -5.173180830302138e-02 5.064302371085747e-02 8.550406147725537e-02 ...
   %  -2.965170712303975e-01 4.008138273429261e-01 -2.965170712303975e-01 ...
   %  8.550406147725538e-02 5.064302371085747e-02 -5.173180830302140e-02 ...
   %  8.437182327895392e-18 2.011451899152559e-02 -7.221104373597303e-03 ...
   %  -3.933584849155314e-03 3.455252881204604e-03 -1.249952937465984e-18]; 
   % HRWT = ftrans2(HP6);
   % save ftrans2 HRWT -APPEND ,
    load ftrans2 HRWT ;
    noise = filter2(HRWT,whitenoise);
 otherwise % noise matrix
   disp('Read noise matrix')
   noise1 = imread('ruma.tif','tif');
   noise1 = double(noise1);
   noise1 = noise1(1:size(clean,1),1:size(clean,2));
   meannoise = sum(noise1(:))/prod(size(noise1));
   noise = noise1 - meannoise;
end

% make noisy image

img = clean + noise;
noisefree = clean;

if what_noise, ratio = snr2(clean,noise), end

imgmap = [(0:255)' (0:255)' (0:255)']/255;
figure(1),clf,
  image(img); colormap(imgmap); title('The noisy image')

% select a wavelet filter

which_filter = input('Which filter\n 1 Haar\n 2 Symm CDF(4,4)\n 3 ON Daubechies\n');
if isempty(which_filter), which_filter = 1; end

switch which_filter
  case 2
   % generate (9,7) = CDF(4,4) symm filter
   disp('Symmetric CDF(4,4) = (9,7) filter')
   [qmf,dqmf] = MakeBSFilter('CDF',[4 4]); %WaveLab function
   [h,g,ht,gt,sh,sg,sht,sgt] = qmf2hgs(qmf,dqmf);
  case 3
   disp('Orthonormal Daubechies filter length 4')
   h = MakeONFilter('Daubechies',4);
   [h, g, ht, gt, sh, sg, sht, sgt] = qmf2hgs(h);
  otherwise
   disp('Haar filter')
   h = MakeONFilter('Haar');
   [h, g, ht, gt, sh, sg, sht, sgt] = qmf2hgs(h);
end

%ht,sht,gt,sgt,h,sh,g,sg

% select the levels of wavelet transform

N = size(img,1);
M = input('Level of transform [2 3] ');
if isempty(M), M = 2; end

% select rudundant or subsampled transform

which_WT = input('redundent (0) or not (1)? ');
if isempty(which_WT), which_WT = 0; end

switch which_WT
  case 1
   disp('Level dependent thresholding on decimated WT')
   fwtgcv
  otherwise
   disp('Level dependent thresholding on non-decimated WT')
   %which_threshold = input('Which threshold?\n 1 GCV\n 2 Oracle\n 3 Ising\n');
   which_threshold = input('Which threshold?\n 1 GCV\n 2 Oracle\n');
   if isempty(which_threshold), which_threshold = 1; end

    switch which_threshold
      %case 3
      %  disp('Ising threshold')
      %  disp('Warning: this may take a while')
      %  rwtising
      case 2
        disp('Oracle threshold')
        rwtoracle
      otherwise
        disp('GCV threshold')
        rwtgcv
    end
end
ratio = snr2(noisefree,imgout-clean)
  
if ~which_WT

%showlabels

  figure(3),clf
  thumb = IRT2_BIO(wout(3:3,:,:,:),h,g,sh,sg,0.5)/4;
  figure(3),subplot(2,2,1),image(thumb), colormap(imgmap),
  title('Coarsest level M')
  
  labelmap = [(0:255)' (0:255)' (0:255)']/255;
  x1 = zeros(size(img));
  x1(:,:) = ~label(j,1,:,:)*256;
  figure(3),subplot(2,2,2), image(x1), colormap(labelmap)
  title('labels, horizontal')
  
  x2 = zeros(size(img));
  x2(:,:) = ~label(j,2,:,:)*256;
  figure(3),subplot(2,2,3), image(x2), colormap(labelmap)
  title('labels, vertical')

  x3 = zeros(size(img));
  x3(:,:) = ~label(j,3,:,:)*256;
  figure(3),subplot(2,2,4), image(x3), colormap(labelmap)
  title('labels, diagonal')

  j = j-1;

  pause

  W(:,:) = wcoef(j,2,:,:); %coarsest-1 vertical detail
  mma=max(max(W));mmi=min(min(W)); Wimg=(W-mmi)/(mma-mmi)*256;
  figure(4),clf,
  figure(4),subplot(2,2,1), image(Wimg), colormap(imgmap),
  title('WT vertical subband level M-1')
  
  [Wthr, thr] = mingcv2(W);

  X = (abs(W) > thr);
  figure(4),subplot(2,2,3), image(~X*256), colormap(labelmap),
  title('wavelet coefs above thr')
  X = (abs(W) > thr*3);
  figure(4),subplot(2,2,4), image(~X*256), colormap(labelmap),
  title('wavelet coefs above 3*thr')
end %if ~which_WT
