% alignment:
%
% Sctipt which aligns image mix2k relative to mix1k (calls: "imalign_" function)
%
% Images after alignment:  mix1a , mix2a 
% Control variables:
%   ctr_fig  - at 1 - enables to see figures,
%              at 0 - no firgures are printed. Set at 0.
%   delta_al1 - maximal aligment size (in pixels).  Set at 6 (pixels).
%   rez       - aligment resolution.  Set at 4 (resolution of 1/4 pixel).


if exist('ctr_fig'); ctr_fig0 = ctr_fig; end

% Control variables:
ctr_fig = 0;  %  at 1 - enables to see figures,
              %  at 0 - no firgures are printed.
delta_al1=6;  % max aligment size (in pixels)
rez=4;        % aligment resolution (1/rez pixels)
delta_al2=rez;


% % 1st alignment
fprintf('1st alignment:\n') 
[mix2a1,sh,sv]=imalign_(mix2k,mix1k, delta_al1 );
fprintf('(end 1st alignment)\n')
if ctr_fig
    figure, imagesc( mix1k-mix2k), colormap gray , title('Initial')
    figure, imagesc( mix1k-mix2a1), colormap gray , title('1st alignment')
    figure, imagesc(sh), colormap gray, title('1st alignment - Hor')
    figure, imagesc(sv), colormap gray, title('1st alignment - Vert')
end
clear sh sv mix2k


% % Enlargement
fprintf('Enlargement: ') 
mix1r=imresize(mix1k,rez,'bicubic'); fprintf('1, ') 
mix2r=imresize(mix2a1,rez,'bicubic'); fprintf('2  ') 
fprintf('(end enlargement)\n') 
clear  mix1k mix2a1


% % 2nd alignment
fprintf('2nd alignment: (fine alignment) \n') 
[mix2ra,sh2,sv2]=imalign_(mix2r,mix1r, delta_al2 );
fprintf('(end 2nd alignment)\n') 
if ctr_fig
    figure, imagesc(sh), colormap gray, title('2nd alignment - Hor')
    figure, imagesc(sv), colormap gray, title('2nd alignment - Vert')
end

clear sh sv 

% % Reduction
fprintf('Reduction: ') 
mix2a=imresize(mix2ra,1/rez,'bicubic');fprintf('1, ') 
mix1a=imresize(mix1r,1/rez,'bicubic'); fprintf('2  ')
fprintf('(end reduction)\n') 

if ctr_fig
figure, imagesc( mix1a-mix2a), colormap gray , title('Final aligment')
end

clear  mix2ra  mix2r

if exist('ctr_fig0'); ctr_fig = ctr_fig0; end
fprintf('(end aligment) \n') 
