% function displayTemplateAndMatching

% Display active basis and adaboost templates;
% and display matched templates on example images.

sx = 100; sy = 150;

% Change the following parameter to control how many features to show for the active basis template. 
nF = 60; % show first 30 Gabor elements

%% active basis template from 5 training positives
iRun = 4;
addpath('sigmoidAdaptive/');
if ~exist(sprintf('sigmoidAdaptive/model_run%d.mat',iRun),'file')
    cd('sigmoidAdaptive/');
    tilt(iRun);
    ablearn(iRun);
    cd('../');
end
load(sprintf('sigmoidAdaptive/model_run%d.mat',iRun));

scale = .7;  % scale of Gabors
norient = 15;  % number of orientations
[allfilter, allsymbol] = makefilter(scale, norient);  % generate Gabor filters 


Mx = Mx(1:nF); My = My(1:nF); Mi = Mi(1:nF); Mm = Mm(1:nF); Mm1 = Mm1(1:nF); Totalsketch = nF;
sym = zeros(sx,sy);
for k = 1:nF
    ori = Mi(k) + 1;
    col = My(k);
    row = Mx(k);
    sym = drawGaborSymbol( sym, allsymbol, row, col, ori, 1 );
end
towrite = -sym;
towrite = uint8(255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));

imwrite(towrite,sprintf('ActiveBasisTemplate_sketch%d.png',nF));
[y x c] = size(towrite);
figure('Units','Pixels','Resize','off',...
'Position',[100 100 x y],'PaperUnit','points',...
'PaperPosition',[0 0 x y]);
axes('position',[0 0 1 1]);
imshow(towrite, []);
axis off
saveas(gcf, sprintf('ActiveBasisTemplate_sketch%d.eps',nF), 'eps');


%% active basis template matching
load(sprintf('RandomSplits/split%d.mat',iRun),'posTrainImages');
addpath('sigmoidAdaptive');
posFolder = 'posAll/';  % folder of positive testing images
posTestImages = posTrainImages;
posTestImagePath = cell(1,length(posTestImages));
for i = 1:length(posTestImages)
    posTestImagePath{i} = [posFolder posTestImages{i}];
end
% posTestImagePath = posTestImagePath(1:19);
nimage = length(posTestImagePath); % # images
Isize = zeros(nimage, 2);
I = cell(1,nimage);
for i = 1 : nimage
    tmpIm = imread(posTestImagePath{i});
    if size(tmpIm,3) == 3
        tmpIm = rgb2gray(tmpIm);
    end
    I{i} = double(tmpIm); 
    Isize(i, :) = size(I{i});
end
sx = min(Isize(:, 1)); sy = min(Isize(:, 2)); 
for i = 1 : nimage
    I{i} = I{i}(1:sx, 1:sy); 
end   % make the sizes of the testing images the same

Sx = zeros(1, nimage) + sx; 
Sy = zeros(1, nimage) + sy; 

% filter the testing images 
disp('start filtering');
tic
allfiltered = applyfilterfftsame(I, allfilter);  % filter testing images
disp(['filtering time: ' num2str(toc) ' seconds']);

% sigmoid transformation
Ctransform(nimage, norient, allfiltered, Sx, Sy, Upperbound);

% prepare the spaces for C code
Asym = cell(1, nimage);   % symbolic plot for each image with active Gabor
for i = 1 : nimage
    Asym{i} = zeros(size(I{1}));  
end
tss = zeros(nimage, Totalsketch);  % total scores for testing images

% pass on the pointers to C for heavy computation
disp('start testing');
tic
bkp = zeros([nimage norient]);
Ctesting(nimage, norient, allfiltered, C, h, sx, sy,...
    Mi, Mx, My, Mm, Mm1, allsymbol(1, :), Asym, Lrange,...
    Orange, sub, Totalsketch, Upperbound, tss, M, lam, e, lz, bkp);

K = nimage;
[tsssort, ind] = sort(tss);
J = cell(1,nimage*2+2);
J{1} = -sym;
J{2} = [];
for i = 1 : K 
    J{2*i+1} = I{ind(nimage-i+1)};
    J{2*i+2} = -(Asym{ind(nimage-i+1)}); 
end
towrite = displayImages(J,nimage*2+2,sx,sy);
imwrite(towrite,'matched.png');

close
[y x c] = size(towrite);
figure;imshow(towrite, []);
print('-depsc',sprintf('matched.eps') );


