# LearnEllipsoidTemplate

## PURPOSE

LearnEllipsoidTemplate - Learns an active basis template for Ellipsoid

## SYNOPSIS

This is a script file.

## DESCRIPTION

     LearnEllipsoidTemplate - Learns an active basis template for Ellipsoid

## CROSS-REFERENCE INFORMATION

This function calls:

- GaborFilter GaborFilter - generate Gabor filter at fixed scale and orientation.

This function is called by:

## SOURCE CODE

    0001 % LearnEllipsoidTemplate - Learns an active basis template for Ellipsoid
    0002
    0003 clear all; close all;
    0004 disp('===============> Learning the active basis model from positive training images');
    0005
    0006 load ../template_params
    0007
    0008 %% generate the training image
    0009 numImage = 1; % number of training images
    0010 I = cell(numImage, 1);
    0011 sizex = 63; sizey = 63;
    0012 center = 32; a = 22; b = 10;
    0013 I0 = single(zeros(sizex, sizey));
    0014 for x = 1:sizex
    0015         for y = 1:sizey
    0016             if (((x-center)/a)^2+((y-center)/b)^2<1.)
    0017                 I0(x, y) = single(1.);
    0018             end
    0019         end
    0020 end
    0021 I{1} = I0+0.;
    0022
    0023 allSizex = zeros(1, numImage, 'single')+sizex;
    0024 allSizey = zeros(1, numImage, 'single')+sizey;
    0025 %% learning a template
    0026
    0027
    0028 % use the 3rd 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 = ApplyfilterfftsameLocalNormPerOri(I, GaborFilter((s-1)*nGaborOri+(1: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
    0086     %% Display and save results
    0087 %         OutLearningResult;
    0088 %         Outepsgif(-symbolicTemplate,
    0089 %         ['output/learnedTemplateUsingGaborScale' num2str(s)]); % output
    0090 %         learned template
    0091
    0092 disp(['mex-C learning time: ' num2str(toc) ' seconds']);
    0093 templateName = ['../result/learnedEllipsoidTemplate' num2str(s-1)];
    0094 save(templateName, 'numElement', 'selectedOrient', 'selectedx', 'selectedy', 'selectedScale', 'selectedlambda', 'selectedLogZ', ...
    0095                    'sizex', 'sizey', 'symbolicTemplate', 'subsampleM1');
    0096