

%% parameters for initialization and iteration
useWholeImageOrNot = -1; % whether use the whole image of I{start} or use a portion of it to initialize learning
multipleResolutionImageName = ['working/multipleResolutionImage' num2str(starting)]; 
load(multipleResolutionImageName, 'J0');
tmp = inscribe(templateSize,J0,0);  % currently, there is only one resolution
if (useWholeImageOrNot>0)  % initialize from the whole image 
    startx = 1; starty = 1;
    [endx endy] = size(tmp);
end
I0 = tmp(startx:endx, starty:endy); % initialize the template from the starting image 
[sizeTemplatex sizeTemplatey] = size(I0); % size of template
halfTemplatex = floor(sizeTemplatex/2.); halfTemplatey = floor(sizeTemplatey/2.); % half size
sizeTemplatexSubsample = floor(sizeTemplatex/subsample); % subsample size
sizeTemplateySubsample = floor(sizeTemplatey/subsample);

%% spaces for learning
SUM1mapLearn = cell(numImage, numOrient); % data structures for learning
MAX1mapLearn = cell(numImage, numOrient); 
trackMapLearn = cell(numImage, numOrient);
for img = 1:numImage
    for orient = 1:numOrient
       SUM1mapLearn{img, orient} = single(zeros(sizeTemplatex, sizeTemplatey)); 
       MAX1mapLearn{img, orient} = single(zeros(sizeTemplatexSubsample, sizeTemplateySubsample)); 
       trackMapLearn{img, orient} = single(zeros(sizeTemplatexSubsample, sizeTemplateySubsample)); 
    end
end

%% variables for learning
selectedOrient = zeros(1, numElement1);  % orientation and location of selected Gabors
selectedx = zeros(1, numElement1); 
selectedy = zeros(1, numElement1);
selectedlambda = zeros(1, numElement1); % weighting parameter for scoring template matching
selectedLogZ = zeros(1, numElement1); % normalizing constant
commonTemplate = single(zeros(sizeTemplatex, sizeTemplatey)); % template of active basis 
deformedTemplate = cell(1, numImage); % templates for training images 
for img = 1 : numImage
    deformedTemplate{img} = single(zeros(sizeTemplatex, sizeTemplatey));  
end
allSizex0 = sizeTemplatex + zeros(1, numImage); allSizey0 = sizeTemplatey + zeros(1, numImage); 
learningSpaceName = ['working/learningSpace' '_' num2str(startx) '_' num2str(starty)];
save(learningSpaceName, 'SUM1mapLearn', 'MAX1mapLearn', 'trackMapLearn'); 
learningSpaceMoreName = ['working/learningSpaceMore' '_' num2str(startx) '_' num2str(starty)]; 
save(learningSpaceMoreName, 'commonTemplate', 'deformedTemplate'); 
learnedTemplateName = ['working/learnedTemplate' '_' num2str(startx) '_' num2str(starty)]; 
save(learnedTemplateName, 'selectedOrient', 'selectedx', 'selectedy', 'selectedlambda', 'selectedLogZ');             

%% Output options
showMAX2SUM2 = 1;
showOriginal = 1;


