% SUM-MAX MAPS FOR TEMPLATE MATCHING INFERENCE 
%% load in learned template, set parameter values
clear; close all; 
load 'learnedTemplate'; % load in the learned template
mex Caverage.c; % sigmoid transformation
mex CgetMAX1track.c;   % getting MAX1 maps without getting track maps
mex CgetSUM2.c;   % template matching inference
%% load in test image, generate multiple resolutions
numResolution = 10;  % total number of resolutions to search 
allSizex = zeros(1, numResolution); allSizey = zeros(1, numResolution); 
tmpIm = imread('test.jpg'); 
if size(tmpIm,3) == 3
     tmpIm = rgb2gray(tmpIm);
end
J0 = single(tmpIm); 
MAX1maps = cell(numResolution, numOrient); 
trackMap = cell(numResolution, numOrient);
SUM2map = cell(1, numResolution); 
for(img=1:numResolution)
    J{img} = imresize(J0, .20+(img-1)*.03, 'nearest');  % scaled images
    [sizex, sizey] = size(J{img}); 
    allSizex(img) = sizex; allSizey(img) = sizey; % sizes of images at multiple resolutions
    sizexSubsample = floor(sizex/subsample); sizeySubsample = floor(sizey/subsample); 
    SUM2map{img} = single(zeros(floor((sizex+sizeTemplatex)/subsample), floor((sizey+sizeTemplatey)/subsample)));  % log-likelihood score maps for testing images
    averageMap{img} = single(zeros(sizex, sizey));
    for (orient=1:numOrient)
        MAX1map{img, orient} = single(zeros(sizexSubsample, sizeySubsample)); 
        trackMap{img, orient} = single(zeros(sizexSubsample, sizeySubsample));
    end
    translatedTemplate{img} = single(zeros(sizex, sizey));  % symbolic sketch on testing image
end
MAX2score = single(zeros(1, numResolution));  % maximum log-likelihood score at each resolution
Fx = zeros(1, numResolution); Fy = zeros(1, numResolution); % maximum likelihood location at each resolution
%% compute SUM1 maps for all resolutions
disp(['start filtering']); tic
SUM1map = ApplyFilterfft(J, allFilter, localOrNot, localHalfx, localHalfy);  % filter testing image at all resolutions
disp(['filtering time: ' num2str(toc) ' seconds']);
%% compute the averageMap
Caverage(numResolution, allSizex, allSizey, numOrient, halfFilterSize, SUM1map, averageMap, templateHalfx, templateHalfy, windowNormalizeOrNot);
%% compute MAX1 maps for all resolutions
disp(['start maxing']); tic
CgetMAX1track(numResolution, allSizex, allSizey, numOrient, locationShiftLimit, orientShiftLimit, subsample, ...
              SUM1map, MAX1map, trackMap); 
disp(['maxing time: ' num2str(toc) ' seconds']);
%% detect the object and superpose template
disp(['start finding']); tic
CgetSUM2(numResolution, allSizex, allSizey, numOrient, ...
         locationShiftLimit, orientShiftLimit, subsample, ...
         sizeTemplatex, sizeTemplatey, halfFilterSize, allSymbol(1, :), ...
         numElement, selectedOrient, selectedx, selectedy, selectedlambda, selectedLogZ, ...
         MAX1map, trackMap, SUM2map, translatedTemplate, MAX2score, Fx, Fy, ...
         averageMap, saturation);
%% display results
[maxOverResolution ind] = max(MAX2score);   % most likely resolution
Outepsgif(tmpIm, 'ex2_5_sw_original'); % original image
Outepsgif(J{ind}+translatedTemplate{ind}*100, 'ex2_5_sketch'); % overlaid sketch at optimal resolution  

figure('Position', [1 1 1024 768]); 
mesh(double(SUM2map{ind})); % log-likelihood score map at optimal resolution
% adjust the display format and save the plot
set(gca,'FontSize',18);
set(gcf, 'PaperPositionMode', 'auto');
print('-depsc', 'ex2_3_S2_sw.eps');

figure; h1=plot(MAX2score); 
title('MAX2 score vs scale'); xlabel('scale'); ylabel('MAX2');
% adjust the display format and save the plot
set(gca,'FontSize',16);
set(get(gca,'XLabel'),'FontSize',18);
set(get(gca,'YLabel'),'FontSize',18);
set(get(gca,'Title'),'FontSize',20);
set(h1,'Linewidth',3);
set(gcf, 'PaperPositionMode', 'auto');
print('-depsc', 'ex2_3_M2_sw.eps');





