%%% Learning from the cropped images 
close all; 
for (s = 1:numScale)
  learningSpaceName = ['working/learningSpace' num2str(s)];  
  load(learningSpaceName);
  learningSpaceMoreName = ['working/learningSpaceMore' num2str(s)];  
  load(learningSpaceMoreName); 
  learnedTemplateName = ['working/learnedTemplate' num2str(s)];  
  load(learnedTemplateName); 
  storeExponentialModelName = ['working/storedExponentialModel' num2str(s)]; 
  load(storeExponentialModelName); 
  SUM1MAX1mapName = ['working/SUM1MAX1map' 'image' num2str(starting) 'scale' num2str(s)];  
  load(SUM1MAX1mapName); 
  sizex = allSizex(originalResolution); sizey = allSizey(originalResolution); 
  sizexSubsample = floor(sizex/subsample); sizeySubsample = floor(sizey/subsample); 
  ave = averageMap{originalResolution}; 
  averageHere = ave(startx+halfTemplatex, starty+halfTemplatey); % average within local window
  for (orient = 1 : numOrient)      
       Ccopy(SUM1mapLearn{starting, orient}, SUM1map{originalResolution, orient}, startx-1, starty-1, sizeTemplatex, sizeTemplatey, sizex, sizey, averageHere, saturation); 
      % Ccopy(MAX1mapLearn{starting, orient}, MAX1map{originalResolution, orient}, round((startx-1)/subsample), round((starty-1)/subsample), sizeTemplatexSubsample, sizeTemplateySubsample, sizexSubsample, sizeySubsample, averageHere, saturation); 
      % Ccopy0(trackMapLearn{starting, orient}, trackMap{originalResolution, orient}, round((startx-1)/subsample), round((starty-1)/subsample), sizeTemplatexSubsample, sizeTemplateySubsample, sizexSubsample, sizeySubsample); 
  end
  CgetMAX1track(numImage, allSizex0, allSizey0, numOrient, locationShiftLimit, orientShiftLimit, subsample, ...
                SUM1mapLearn, MAX1mapLearn, trackMapLearn);
  disp(['start learning at Gabor length ' num2str(halfFilterSize*2+1)]); tic
  CsharedSketch(numOrient, locationShiftLimit, orientShiftLimit, subsample, ... % about active basis  
       numElement, numImage, sizeTemplatex, sizeTemplatey, ...
       SUM1mapLearn, MAX1mapLearn, trackMapLearn, ... % about training images 
       halfFilterSize, Correlation, allSymbol(1, :), ... % about filters
       numStoredPoint, storedlambda, storedExpectation, storedLogZ, ... % about exponential model 
       selectedOrient, selectedx, selectedy, selectedlambda, selectedLogZ, ... % learned parameters
       commonTemplate, deformedTemplate); % learned templates 
disp(['mex-C learning time: ' num2str(toc) ' seconds']);
Outepsgif(-double(commonTemplate), ['LNlion_sym' num2str(it)]); 
learnedTemplateName = ['working/learnedTemplate' num2str(s)]; 
save(learnedTemplateName, 'numElement', 'selectedOrient', 'selectedx', 'selectedy', 'selectedlambda', 'selectedLogZ'); 
tmp = Ioriginal{starting}; 
tmp(startx:endx, starty:endy) = tmp(startx:endx, starty:endy) + deformedTemplate{starting}*100.; 
Outepsgif(tmp, ['output/LNlion' num2str(starting)]); 
end