# generateTransformedS2Templates

## PURPOSE

generateTransformedS2Templates - Affine transforms of the S2

## SYNOPSIS

This is a script file.

## DESCRIPTION

     generateTransformedS2Templates - Affine transforms of the S2
       templates (shape motifs).

## CROSS-REFERENCE INFORMATION

This function calls:

- displayImages displayImage - display a set of images of the same size (bx, by) on
- displayTemplate displayTemplate - Display the symbolic template of an active basis template.
- templateAffineTransform templateAffineTranform - Affine transform of vertices in the template.

This function is called by:

- StartFromHere StartFromHere - The entry point of this project.

## SOURCE CODE

    0001 % generateTransformedS2Templates - Affine transforms of the S2
    0002 %   templates (shape motifs).
    0003 clear
    0004 load ../template_params nGaborOri nGaborScale GaborSymbol
    0005 templateName = ['../result/learnedS2Template'];
    0006 load(templateName);
    0007 nS2T = length(S2Template);
    0008
    0009 %% enumerate the transformations
    0010 count = 0;
    0011 for templateScaleInd = -2:1 % large scale change
    0012     for rotation = 0:2:(nGaborOri-1) % 0 ~ pi is enough for ellisoid
    0013         for rowScale = 2.^[0, 1/4] % small scale change and aspect ratio change
    0014             for colScale = 2.^[1/4, 0]
    0015                 count = count + 1;
    0016                 templateTransform{count} = [templateScaleInd rowScale colScale rotation];
    0017             end
    0018         end
    0019     end
    0020 end
    0021 nTransform = count;
    0022
    0023 %% the actual transformation computation
    0024 TransformedTemplate = cell(nTransform,nS2T);
    0025
    0026 maxSquareSize = 0;
    0027 for iS2 = 1:nS2T
    0028     selectedx = S2Template{iS2}.selectedx;
    0029     selectedy = S2Template{iS2}.selectedy;
    0030     selectedOrient = S2Template{iS2}.selectedOrient;
    0031     selectedScale = S2Template{iS2}.selectedScale;
    0032     numElement = S2Template{iS2}.numElement;
    0033     for iT = 1:nTransform
    0034         templateScaleInd = templateTransform{iT}(1);
    0035         rowScale = templateTransform{iT}(2);
    0036         colScale = templateTransform{iT}(3);
    0037         rotation = templateTransform{iT}(4);
    0038         tmpSelectedRow = zeros(numElement,1,'single');
    0039         tmpSelectedCol = zeros(numElement,1,'single');
    0040         tmpSelectedOri = zeros(numElement,1,'single');
    0041         tmpSelectedScale = zeros(numElement,1,'single');
    0042         for iF = 1:numElement
    0043             inRow = selectedx(iF);
    0044             inCol = selectedy(iF);
    0045             inO = selectedOrient(iF); % start from 0
    0046             inS = selectedScale(iF);
    0047             [tmpSelectedRow(iF), tmpSelectedCol(iF), tmpSelectedOri(iF) tmpSelectedScale(iF)] = ...
    0048                 templateAffineTransform(templateScaleInd,rowScale,colScale,rotation,...
    0049                 inRow,inCol,inO,inS,nGaborOri);
    0050         end
    0051         TransformedTemplate{iT,iS2}.selectedRow = tmpSelectedRow;
    0052         TransformedTemplate{iT,iS2}.selectedCol = tmpSelectedCol;
    0053         TransformedTemplate{iT,iS2}.selectedOri = tmpSelectedOri;
    0054         TransformedTemplate{iT,iS2}.selectedScale = tmpSelectedScale;
    0055         TransformedTemplate{iT,iS2}.selectedLambda = S2Template{iS2}.selectedlambda;
    0056         newSelectedLogZ = zeros(1,numElement);
    0057         TransformedTemplate{iT,iS2}.selectedLogZ = S2Template{iS2}.selectedLogZ;
    0058         TransformedTemplate{iT,iS2}.subsampleM1 = S2Template{iS2}.subsampleM1;
    0059         TransformedTemplate{iT,iS2}.sym = displayTemplate( ...
    0060             tmpSelectedRow,tmpSelectedCol,tmpSelectedOri, tmpSelectedScale, ones(numElement,1),...
    0061             GaborSymbol,nGaborOri,S2Template{iS2}.subsampleM1);
    0062         if size(TransformedTemplate{iT,iS2}.sym,1) > maxSquareSize
    0063             maxSquareSize = size(TransformedTemplate{iT,iS2}.sym,1);
    0064         end
    0065     end
    0066 end
    0067
    0068 %% display the transformed templates
    0069 for iS2 = 1:nS2T
    0070     tmpTT = cell(1,nTransform);
    0071     for iT = 1:nTransform
    0072         % inscribe into a fixed-size square
    0073         tmpTT{iT} = zeros(maxSquareSize,maxSquareSize);
    0074         squareSize = size(TransformedTemplate{iT,iS2}.sym,1);
    0075         margin = floor((maxSquareSize - squareSize)/2);
    0076         tmpTT{iT}(margin+(1:squareSize),margin+(1:squareSize)) = -single(TransformedTemplate{iT,iS2}.sym);
    0077     end
    0078     im = displayImages(tmpTT,4*2,50,50);
    0079     imwrite(im,sprintf('../result/transformedTemplates_%d.png',iS2));
    0080     figure; imshow(im,[]);
    0081     saveas(gcf,sprintf('../result/transformedTemplates_%d.eps',iS2), 'epsc');
    0082     close;
    0083 end
    0084
    0085 %% compute pairwise correlation
    0086 load ../template_params CorrGabor
    0087 save ../result/transformedS2Template.mat TransformedTemplate templateTransform
    0088
    0089
    0090