%===============================================================================
%
% Demo program for the wavelet decomposition and reconstruction
% of an image, using nondecimated transform with the quadratic spline wavelet.
% Implementation with three detail images at each resolution scale.
%
% File name: wt_3det.m
% Last modification Date:	09/04/2001	
%
% Author: Aleksandra Pizurica <sanja@telin.ugent.be> 
%         Ghent University 
%         Dept. Telecommunications and Information Processing  
%
% Copyright: All software, documentation, and related files in this distribution
%            are Copyright (c) 2001 Aleksandra Pizurica. All rights reserved.
%
%
% Permission is granted for use and non-profit distribution providing that this
% notice be clearly maintained. The right to distribute any portion for profit
% or as part of any commercial product is specifically reserved for the author.
%===============================================================================



clear all;
close all;

global InputType SCAN filename;
 
InputType=0;
 
while (InputType~=1 & InputType~=2 & InputType~=3 & InputType~=4)  
   InputType=input('\nInput image format:\n1. "*.tif"\n2. "*.bmp"\n3. "*.mat"\n4. "*.bin"\n>>  ');
end
imload;
X=SCAN;


if(InputType==4)
    X=background_removal(X);
    X=normalize(X);
    while (LogTransf~='y' & LogTransf~='n')
       LogTransf=input('\nPerform logarithmic transform? (y/n)','s');
    end
    if (LogTransf=='y');
       X=log(X+1);
    end
end

%Decomposition
  
  [A1,HL1,LH1,HH1]=wt3det_spline(X,0);   
  [A2,HL2,LH2,HH2]=wt3det_spline(A1,1);   
  [A3,HL3,LH3,HH3]=wt3det_spline(A2,2); 
  
% Correction factors from Mallat&Zhong'92
% needed for exact estimation of the Lipschitz regularity
  
lambda1=1.5;
lambda2=1.12;
lambda3=1.03;
lambda4=1.01;
%=============

HL1=HL1/lambda1;
HL2=HL2/lambda2;
HL3=HL3/lambda3;

LH1=LH1/lambda1;
LH2=LH2/lambda2;
LH3=LH3/lambda3;

HH1=HH1/lambda1;
HH2=HH2/lambda2;
HH3=HH3/lambda3;
  
  
  
%Reconstruction
  
   A2r=iwt3det_spline(A3,lambda3*HL3,lambda3*LH3,lambda3*HH3,2);
   A1r=iwt3det_spline(A2r,lambda2*HL2,lambda2*LH2,lambda2*HH2,1);
   A0r=iwt3det_spline(A1r,lambda1*HL1,lambda1*LH1,lambda1*HH1,0);
  
  figure('Units','normalized','Position',[0.1 0.3 0.5 0.5]);
  subplot(3,3,1),imagesc(HL1);
  axis off;
  title('HL1');
  subplot(3,3,2),imagesc(LH1);
  axis off;
  title('LH1');
  subplot(3,3,3),imagesc(HH1);
  axis off;
  title('HH1');
  subplot(3,3,4),imagesc(HL2);
  axis off;
  title('HL2');
  subplot(3,3,5),imagesc(LH2);
  axis off;
  title('LH2');
  subplot(3,3,6),imagesc(HH2);
  axis off;
  title('HH2');
  subplot(3,3,7),imagesc(HL3);
  axis off;
  title('HL3');
  subplot(3,3,8),imagesc(LH3);
  axis off;
  title('LH3');
  subplot(3,3,9),imagesc(HH3);
  axis off;
  title('HH3');
  colormap(gray);

  
  figure('Units','normalized','Position',[0.62 0.3 0.25 0.5]);
  subplot(2,1,1),imagesc(X);
  title('Original image');
  subplot(2,1,2),imagesc(A0r);
  title('Reconstructed image');
  colormap(gray);
  
 
  
    
  
  


