# LearnS2Template

## PURPOSE

LearnS2Template - Learns an active basis template for Ellipsoid, angle,

## SYNOPSIS

This is a script file.

## DESCRIPTION

     LearnS2Template - Learns an active basis template for Ellipsoid, angle,
       and parallel bars.

## CROSS-REFERENCE INFORMATION

This function calls:

- GaborFilter GaborFilter - generate Gabor filter at fixed scale and orientation.
- applyfilterMultiScale applyfilterMultiScale - Filter image by a bank of multiscale filters, with local normalization.
- displayTemplate displayTemplate - Display the symbolic template of an active basis template.

This function is called by:

- StartFromHere StartFromHere - The entry point of this project.

## SOURCE CODE

    0001 % LearnS2Template - Learns an active basis template for Ellipsoid, angle,
    0002 %   and parallel bars.
    0003
    0004 clear all; close all;
    0005 disp('===============> Learning the active basis model from positive training images');
    0006
    0007 load ../template_params
    0008
    0009 %% Template 1: Ellipsoid
    0010 % generate the training image for the first template: ellisoid
    0011 numImage = 1; % number of training images
    0012 I = cell(numImage, 1);
    0013 sizex = 63; sizey = 63;
    0014 center = 32; a = 22; b = 10;
    0015 I0 = single(zeros(sizex, sizey));
    0016 for x = 1:sizex
    0017         for y = 1:sizey
    0018             if (((x-center)/a)^2+((y-center)/b)^2<1.)
    0019                 I0(x, y) = single(1.);
    0020             end
    0021         end
    0022 end
    0023 I{1} = I0+0.;
    0024
    0025 allSizex = zeros(1, numImage, 'single')+sizex;
    0026 allSizey = zeros(1, numImage, 'single')+sizey;
    0027
    0028 % use the 4th scale
    0029 s = 4;
    0030 storeName = ['../result/naturalStat' num2str(s-1)];
    0031 load(storeName);
    0032 numElement = 14;
    0033 halfFilterSize = ( size(GaborFilter{(s-1)*nGaborOri+1},1) - 1 ) / 2;
    0034 disp(['start filtering training images at Gabor length ' num2str(2*halfFilterSize+1)]); tic
    0035
    0036 selectedOrient = zeros(numElement, 1, 'single');  % orientation and location of selected Gabors
    0037 selectedx = zeros(numElement, 1, 'single');
    0038 selectedy = zeros(numElement, 1, 'single');
    0039 selectedlambda = zeros(numElement, 1, 'single'); % weighting parameter for scoring template matching
    0040 selectedLogZ = zeros(numElement, 1, 'single'); % normalizing constant
    0041 symbolicTemplate = zeros(sizex, sizey, 'single'); % template of active basis
    0042
    0043
    0044 % Compute SUM1 maps by Gabor filtering
    0045 SUM1map = applyfilterMultiScale(I, GaborFilter((s-1)*nGaborOri+(1:nGaborOri)), nGaborOri); % SUM1 maps by Gabor filtering
    0046 disp(['filtering time: ' num2str(toc) ' seconds']);
    0047
    0048 % Compute MAX1 maps and record the track maps
    0049 locationShiftLimit = 0; % shift in normal direction = locationShiftLimit*subsample pixels
    0050 orientShiftLimit = 0; % shift in orientation
    0051 Csigmoid(numImage, allSizex, allSizey, nGaborOri, GaborSaturation, SUM1map);
    0052 MAX1map = cell(numImage, nGaborOri);
    0053 subsampleM1 = 1;  % to be safe, please refrain from setting it to be larger than 1
    0054 trackMap = cell(numImage, nGaborOri);
    0055 for img = 1:numImage
    0056     for orient = 1:nGaborOri
    0057         MAX1map{img, orient} = single(zeros(floor(sizex/subsampleM1), floor(sizey/subsampleM1)));
    0058         trackMap{img, orient} = single(zeros(floor(sizex/subsampleM1), floor(sizey/subsampleM1)));
    0059     end
    0060 end
    0061 disp(['start maxing training images at Gabor length ' num2str(2*halfFilterSize+1)]); tic
    0062 CgetMAX1track(numImage, allSizex, allSizey, nGaborOri, locationShiftLimit, orientShiftLimit, subsampleM1, ...
    0063               SUM1map, MAX1map, trackMap);
    0064 disp(['maxing time: ' num2str(toc) ' seconds']);
    0065
    0066
    0067 % Learning by shared sketch algorithm
    0068 learningSampleSize = 1;
    0069 deformedTemplate = cell(1, learningSampleSize); % templates for training images
    0070 for img = 1 : learningSampleSize
    0071     deformedTemplate{img} = single(zeros(sizex, sizey));
    0072 end
    0073 SUM2score = zeros(learningSampleSize, 1); % template matching scores for training images
    0074 disp(['start mex-C learning of template at Gabor length ' num2str(2*halfFilterSize+1)]); tic
    0075 CsharedSketch(nGaborOri, locationShiftLimit, orientShiftLimit, GaborSaturation, subsampleM1, ... % about active basis
    0076        numElement, learningSampleSize, sizex, sizey, ...
    0077        SUM1map, MAX1map, trackMap, ... % about training images
    0078        halfFilterSize, CorrGabor((s-1)*nGaborOri + (1:nGaborOri), (s-1)*nGaborOri + (1:nGaborOri)),...
    0079        GaborSymbol( (s-1)*nGaborOri + (1:nGaborOri) ), ... % about filters
    0080        numStoredPoint, (storedLambda), (storedExpectation), (storedLogZ), ... % about exponential model
    0081        selectedOrient, selectedx, selectedy, selectedlambda, selectedLogZ, SUM2score, ... % learned parameters
    0082        symbolicTemplate, deformedTemplate); % learned templates
    0083
    0084 selectedScale = ones(numElement,1)*(s-1); % the scale index starts from 0
    0085 S2Template{1} = struct('numElement',numElement,'selectedOrient',selectedOrient,...
    0086     'selectedx',selectedx,'selectedy',selectedy,'selectedScale',selectedScale,...
    0087     'selectedlambda',selectedlambda,'selectedLogZ',selectedLogZ,...
    0088     'sizex',sizex,'sizey',sizey,'symbolicTemplate',symbolicTemplate,...
    0089     'subsampleM1',subsampleM1);
    0090
    0091
    0092 %% Learn the second S2 template: angle
    0093 im = imread('angle1.png');
    0094 if size(im,3) == 3
    0095     im = rgb2gray(im);
    0096 end
    0097 im = single(imresize(im,0.15));
    0098 im(im < 250) = 0;
    0099 im(im >= 250) = 1;
    0100 I{1} = im;
    0101 [sizex sizey] = size(im);
    0102 allSizex = single(sizex); allSizey = single(sizey);
    0103 numElement = 14;
    0104 halfFilterSize = ( size(GaborFilter{(s-1)*nGaborOri+1},1) - 1 ) / 2;
    0105 disp(['start filtering training images at Gabor length ' num2str(2*halfFilterSize+1)]); tic
    0106
    0107 selectedOrient = zeros(numElement, 1, 'single');  % orientation and location of selected Gabors
    0108 selectedx = zeros(numElement, 1, 'single');
    0109 selectedy = zeros(numElement, 1, 'single');
    0110 selectedlambda = zeros(numElement, 1, 'single'); % weighting parameter for scoring template matching
    0111 selectedLogZ = zeros(numElement, 1, 'single'); % normalizing constant
    0112 symbolicTemplate = zeros(sizex, sizey, 'single'); % template of active basis
    0113
    0114 % Compute SUM1 maps by Gabor filtering
    0115 SUM1map = applyfilterMultiScale(I, GaborFilter((s-1)*nGaborOri+(1:nGaborOri)), nGaborOri); % SUM1 maps by Gabor filtering
    0116 disp(['filtering time: ' num2str(toc) ' seconds']);
    0117
    0118 % Compute MAX1 maps and record the track maps
    0119 locationShiftLimit = 0; % shift in normal direction = locationShiftLimit*subsample pixels
    0120 orientShiftLimit = 0; % shift in orientation
    0121 Csigmoid(numImage, allSizex, allSizey, nGaborOri, GaborSaturation, SUM1map);
    0122 MAX1map = cell(numImage, nGaborOri);
    0123 subsampleM1 = 1;  % to be safe, please refrain from setting it to be larger than 1
    0124 trackMap = cell(numImage, nGaborOri);
    0125 for img = 1:numImage
    0126     for orient = 1:nGaborOri
    0127         MAX1map{img, orient} = single(zeros(floor(sizex/subsampleM1), floor(sizey/subsampleM1)));
    0128         trackMap{img, orient} = single(zeros(floor(sizex/subsampleM1), floor(sizey/subsampleM1)));
    0129     end
    0130 end
    0131 disp(['start maxing training images at Gabor length ' num2str(2*halfFilterSize+1)]); tic
    0132 CgetMAX1track(numImage, allSizex, allSizey, nGaborOri, locationShiftLimit, orientShiftLimit, subsampleM1, ...
    0133               SUM1map, MAX1map, trackMap);
    0134 disp(['maxing time: ' num2str(toc) ' seconds']);
    0135
    0136
    0137 % Learning by shared sketch algorithm
    0138 learningSampleSize = 1;
    0139 deformedTemplate = cell(1, learningSampleSize); % templates for training images
    0140 for img = 1 : learningSampleSize
    0141     deformedTemplate{img} = single(zeros(sizex, sizey));
    0142 end
    0143 SUM2score = zeros(learningSampleSize, 1); % template matching scores for training images
    0144 disp(['start mex-C learning of template at Gabor length ' num2str(2*halfFilterSize+1)]); tic
    0145 CsharedSketch(nGaborOri, locationShiftLimit, orientShiftLimit, GaborSaturation, subsampleM1, ... % about active basis
    0146        numElement, learningSampleSize, sizex, sizey, ...
    0147        SUM1map, MAX1map, trackMap, ... % about training images
    0148        halfFilterSize, CorrGabor((s-1)*nGaborOri + (1:nGaborOri), (s-1)*nGaborOri + (1:nGaborOri)),...
    0149        GaborSymbol( (s-1)*nGaborOri + (1:nGaborOri) ), ... % about filters
    0150        numStoredPoint, (storedLambda), (storedExpectation), (storedLogZ), ... % about exponential model
    0151        selectedOrient, selectedx, selectedy, selectedlambda, selectedLogZ, SUM2score, ... % learned parameters
    0152        symbolicTemplate, deformedTemplate); % learned templates
    0153
    0154 selectedScale = ones(numElement,1)*(s-1); % the scale index starts from 0
    0155
    0156 S2Template{2} = struct('numElement',numElement,'selectedOrient',selectedOrient,...
    0157     'selectedx',selectedx,'selectedy',selectedy,'selectedScale',selectedScale,...
    0158     'selectedlambda',selectedlambda,'selectedLogZ',selectedLogZ,...
    0159     'sizex',sizex,'sizey',sizey,'symbolicTemplate',symbolicTemplate,...
    0160     'subsampleM1',subsampleM1);
    0161
    0162 %% The third template: a long bar
    0163 selectedx = single([0 0 0 0 0 0 15 15 15 15 15 15]);
    0164 selectedy = single([0 10 20 30 40 50 0 10 20 30 40 50]-30);
    0165 selectedOrient = single([0 0 0 0 0 0 0 0 0 0 0 0]);
    0166 % modified the scale!!!
    0167 selectedScale = ones(numElement,1)*(s-1+1); % the scale index starts from 0
    0168 numElement = length(selectedx);
    0169 selectedlambda = 0.6*ones(numElement, 1, 'single'); % weighting parameter for scoring template matching
    0170 selectedLogZ = 1.2*ones(numElement, 1, 'single'); % normalizing constant
    0171 sym = displayTemplate( ...
    0172             selectedx,selectedy,selectedOrient, selectedScale, ones(numElement,1),...
    0173             GaborSymbol,nGaborOri,subsampleM1);
    0174 S2Template{3} = struct('numElement',numElement,'selectedOrient',selectedOrient,...
    0175     'selectedx',selectedx,'selectedy',selectedy,'selectedScale',selectedScale,...
    0176     'selectedlambda',selectedlambda,'selectedLogZ',selectedLogZ,...
    0177     'sizex',sizex,'sizey',sizey,'symbolicTemplate',sym,...
    0178     'subsampleM1',subsampleM1);
    0179
    0180 %% save results
    0181 disp(['mex-C learning time: ' num2str(toc) ' seconds']);
    0182 templateName = ['../result/learnedS2Template'];
    0183 save(templateName, 'S2Template');
    0184
    0185