close all; clear;
warning off MATLAB:divideByZero
set(0,'DefaultFigureRenderer','zbuffer');

%Parameters
    radius = 2:18;
    g = 128;
    base_radius = 10;
    img = imread('aerial.bmp');
    template = imread('aerial_template.bmp');

template = double(rgb2gray(template));
img = double(rgb2gray(img));

templateSize = floor(size(template)/2);

fftw('measure', size(img,1)); 

% Compute true template position
A = normxcorr2(template, img);
[Q, y] =max(max(A));
[Q, x] =max(A(:,y));
TrueX = floor(x - size(template, 1) / 2);
TrueY = floor(y - size(template, 2) / 2);

% Template modificaitons
    template = imresize(template, 1, 'bicubic');
    template = imrotate(template, 180, 'bilinear');
    template = template ./ max(max(template));
    %template = imnoise(template, 'gaussian');
    
img = img ./ max(max(img));
%img = imnoise(img, 'gaussian', 0, 0.1);
%img = imnoise(img, 'salt & pepper');
output = ones(size(img,1), size(img, 2));

tic
for r=radius
    for angle=1:g
        tmp = imrotate(template, angle/max(g)*360, 'bilinear');
        tmp = imresize(tmp, r/base_radius, 'bicubic');
        A = normxcorr2(tmp, img);
    end
    r
end
toc