% 
% Mariana S.C. Almeida and Luis B. Almeida  (2007)
% e-mail: mariana.almeida@lx.it.pt , luis.almeida@lx.it.pt
% 
% "MAIN_align.m" (sctript) shows how to perform different alignment experiments:
%  'alignment'
%  'blck_alignment' - for large size images (avoid memory problems)
%
%
% The package contains the routines which were used in [1] to align two page images. 
% "MAIN_align.m" shows how to call aligment routines for different experiments.
%
% This code is an edited version of the alignment routines used in [2]
% by Prof. Luís Borges de Almeida: http://www.lx.it.pt/~lbalmeida/research.html
%
% References:
% [1] M. S. C. Almeida and L. B. Almeida, "Wavelet-based separation of nonlinear show through and 
% bleed-through image mixtures", Neurocomputing, vol. 72, pp. 57-70 December 2008. 
%
% [2] L. B. Almeida, “Separating a Real-Life Nonlinear Image Mixture”, 
% Journal of Machine Learning Research, Vol. 6, pp. 1199-1232, 2005.
%
%

clear all

% change here the algnment experiment:
experiment = 'blck_alignment'; 
% experiment = 'alignment'; 


switch lower(experiment)
    case {'blck_alignment'}
        % Load data:
        mix1_str='carta1_al.bmp';
        mix2_str='carta2_al.bmp';
        mix1 = imread(mix1_str);
        mix2 = imread(mix2_str);
        mix1 = mix1(1:end,1:end);
        mix2 = mix2(1:end,1:end);
         
        % Parameters:
        delta_al1=6;  % max aligment size (in pixels)
        rez=4;        % aligment resolution (1/rez pixels)
        nblocos_i = 2; % number of blocks in the vertical-axis   (yy)
        nblocos_j = 2; % number of blocks in the horizontal-axis (xx)

        block_alignment_script
        %   [mix1a mix2a]=block_alignment(mix1, mix2, delta_al1 , rez, nblocos_i, nblocos_j );
        
        figure, imagesc( mix1-mix2), colormap gray , title('Img1-Img2 (Before aligment)')
        figure, imagesc( mix1_a-mix2_a), colormap gray , title('Img1-Img2 (Before aligment)')
        
    case {'alignment'}
        % Load data:
        mix1_str='carta1_al.bmp';
        mix2_str='carta2_al.bmp';
        mix1 = double(imread(mix1_str));
        mix2 = double(imread(mix2_str));
        mix1k = mix1(200:700,200:700);
        mix2k = mix2(200:700,200:700);
        clear mix1 mix2, pack
        
        % Parameters:
        delta_al1=6;  % max aligment size (in pixels)
        rez=4;        % aligment resolution (1/rez pixels)

         alignment_script
         % [mix1a mix2a]=alignment(mix1k, mix2k, delta_al1 , rez);
        
        figure, imagesc( mix1k-mix2k), colormap gray , title('Img1-Img2 (Before aligment)')
        figure, imagesc( mix1a-mix2a), colormap gray , title('Img1-Img2 (Before aligment)')
end
