% A simple example on how to use the stretch filter in order to estimate the stretch between two signals
%
% Author: 
% Konstantinos Bitsakos
% Department of Computer Science 
% University of Maryland, College Park
% kbits@cs.umd.edu
%
% January 2008

% CONSTANTS
bDisplay=1; % Plot the images?
scanline=100;
trueStretch=0.9;

% Load the original image 
im=imresize(imread('tiles_cross.jpg'),0.25);

warpFun=@(x) (trueStretch*x);

% Create a warped version of the original image
[im1, im2, x, nConstant]=gimmeWarpedImage(im,warpFun);

if bDisplay
    % Display the two images
    figure(1), imshow(uint8(im1));
    figure(2), imshow(uint8(im2));
end

% Get the two signals on the scanline
temp1=rgb2gray(uint8(im1));
s1=temp1(scanline,:);
temp2=rgb2gray(uint8(im2));
s2=temp2(scanline,:);

if bDisplay
    % Display the two signals together
    figure(3), plot(x,s1,'-k', x, s2, '-r', 'Linewidth',1.5);
    set(gca,'FontName','FixedWidth', 'FontSize', 17);
    legend('s_1', 's_2');
    xlim([min(x) max(x)]);ylim([0 255]); 
    title('The two signals');
    xlabel('X'); ylabel('Signals');
    set(gcf,'Position',[100 100 1100 400]);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create the filter around the middle of the scanline that measures the stretch  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
u=0.25; w1=-3.5; w2=-1; bettySigmaConst=1;
[H1]=createStretchFilter(floor(length(x)/2), u, w1, w2, bettySigmaConst);

% Measure the stretch with the filter
res1 = conv2(double(s1),H1);
res2 = conv2(double(s2),H1); 
l=length(res1);
dtheta = angle(res1((l+1)/2))-angle(res2((l+1)/2));
dalpha = dtheta/(2*pi*u);
estStretch = exp(dalpha);

fprintf(1,'True Stretch:%.3f\tEstimated Stretch:%.3f\n',trueStretch, estStretch);
