
im = double(imread('./david/imgs/img00001.png'));
h = im(58:58+97,122:122+75);
aver_filt = ones(size(h))./numel(h);

lambda = 0.5;
init_pt = [58, 122];

for i=1:100
    
    % read in image 
    im = double(imread(sprintf('./david/imgs/img%05d.png', i)));
    
    % compute correlation map 
    map_corr = filter2(h, im);
    denom_corr = im.^2;
    denom_corr = sqrt(filter2(aver_filt, denom_corr));
    map_corr = map_corr./denom_corr;
    
    % compute index of best position
    max_idx = find(max(map_corr(:)) == map_corr(:));
    [init_pt(1), init_pt(2)] = ind2sub(size(im), max_idx); 
    init_pt(1) = round(init_pt(1)-(97/2));
    init_pt(2) = round(init_pt(2)-(75/2));
    
    % show results
    h2 = im(init_pt(1):init_pt(1)+97, init_pt(2):init_pt(2)+75);
    imshow(h2, []);
    title(sprintf('%d', i));
    pause;
    
    % update template 
    h = lambda*h+(1-lambda)*h2;
    
end









