function [ estimated_params ] = NGF_registration(x_disp,y_disp )

%% Read the test images
f = imread('rire_t2.jpg');
fs = double( f(:,:,1) );

g = imread('rire_pd.jpg');
gs = double( g(:,:,1) );
%%

%% Deform the image for testing
%  Note: Only translations are considered for reasons explained in report
% x_disp = -3;
% y_disp = -5;

fprintf('Actual parameters = [%d, %d]\n',x_disp, y_disp);

actual_params = [ x_disp; y_disp]';

g = push_forward(fs, actual_params, [1, 1]);

%% 

%% Scaling for multi-grid & Initial guess
scales = [0.125 0.25 0.5 1];
% scales = [1];

initial_guess = [0 0];

figure(1);
%%

%% Bluring the image for better registation
h=[0.05; 0.25; 0.4; 0.25; 0.05]';

f=conv2(h,h,f,'same');
g=conv2(h,h,g,'same');
%%

%% Iterate through each grid level to find optimal solution
for scale = scales
        
    % Scale the images for each grid
    fs = imresize(f, scale);
    gs = imresize(g, scale);

    new_params = initial_guess * scale;

    fs = double(fs);
    gs = double(gs);
 
    % Estimate new parameters using gradient descent
    new_params = gradient_ascent(fs,gs,new_params,[1, 1]*scale);     

    initial_guess = new_params'/ scale;    
end
%%

fprintf('Estimated parameters = [%d, %d]',initial_guess(1), initial_guess(2));

%% Output the estimated parameters
estimated_params = initial_guess;

end

