# generateTransformedTemplates

## PURPOSE

generateTransformedTemplates - Affine transforms of the ellipsoid

## SYNOPSIS

This is a script file.

## DESCRIPTION

     generateTransformedTemplates - Affine transforms of the ellipsoid
       template.

## 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:

## SOURCE CODE

    0001 % generateTransformedTemplates - Affine transforms of the ellipsoid
    0002 %   template.
    0003
    0004 EllipseGaborScale = 4;
    0005 load ../template_params nGaborOri nGaborScale GaborSymbol
    0006 templateName = ['../result/learnedEllipsoidTemplate' num2str(EllipseGaborScale-1)];
    0007 load(templateName);
    0008 height = sizex; width = sizey;
    0009
    0010 %% enumerate the transformations
    0011 count = 0;
    0012 for templateScaleInd = -2:1 % large scale change
    0013     for rotation = 0:2:(nGaborOri-1) % 0 ~ pi is enough for ellisoid
    0014         for rowScale = 2.^[0, 1/4] % small scale change and aspect ratio change
    0015             for colScale = 2.^[1/4, 0]
    0016                 count = count + 1;
    0017                 templateTransform{count} = [templateScaleInd rowScale colScale rotation];
    0018             end
    0019         end
    0020     end
    0021 end
    0022 nTransformedTemplates = count;
    0023
    0024 %% the actual transformation
    0025 TransformedTemplate = cell(nTransformedTemplates,1);
    0026 tmpTT = cell(nTransformedTemplates,1);
    0027 maxSquareSize = 0;
    0028 for iT = 1:nTransformedTemplates
    0029     templateScaleInd = templateTransform{iT}(1);
    0030     rowScale = templateTransform{iT}(2);
    0031     colScale = templateTransform{iT}(3);
    0032     rotation = templateTransform{iT}(4);
    0033     tmpSelectedRow = zeros(numElement,1,'single');
    0034     tmpSelectedCol = zeros(numElement,1,'single');
    0035     tmpSelectedOri = zeros(numElement,1,'single');
    0036     tmpSelectedScale = zeros(numElement,1,'single');
    0037     for iF = 1:numElement
    0038         inRow = selectedx(iF);
    0039         inCol = selectedy(iF);
    0040         inO = selectedOrient(iF); % start from 0
    0041         inS = EllipseGaborScale;
    0042         [tmpSelectedRow(iF), tmpSelectedCol(iF), tmpSelectedOri(iF) tmpSelectedScale(iF)] = ...
    0043             templateAffineTransform(templateScaleInd,rowScale,colScale,rotation,...
    0044             inRow,inCol,inO,inS,nGaborOri);
    0045     end
    0046     TransformedTemplate{iT}.selectedRow = tmpSelectedRow;
    0047     TransformedTemplate{iT}.selectedCol = tmpSelectedCol;
    0048     TransformedTemplate{iT}.selectedOri = tmpSelectedOri;
    0049     TransformedTemplate{iT}.selectedScale = tmpSelectedScale;
    0050     TransformedTemplate{iT}.selectedLambda = selectedlambda;
    0051     newSelectedLogZ = zeros(1,numElement);
    0052     TransformedTemplate{iT}.selectedLogZ = selectedLogZ;
    0053     TransformedTemplate{iT}.subsampleM1 = subsampleM1;
    0054     TransformedTemplate{iT}.sym = displayTemplate( ...
    0055         tmpSelectedRow,tmpSelectedCol,tmpSelectedOri, tmpSelectedScale, ones(numElement,1),...
    0056         GaborSymbol,nGaborOri,subsampleM1);
    0057     if size(TransformedTemplate{iT}.sym,1) > maxSquareSize
    0058         maxSquareSize = size(TransformedTemplate{iT}.sym,1);
    0059     end
    0060 end
    0061
    0062 %% display the transformed templates
    0063 for iT = 1:nTransformedTemplates
    0064     % inscribe into a fixed-size square
    0065     tmpTT{iT} = zeros(maxSquareSize,maxSquareSize);
    0066     squareSize = size(TransformedTemplate{iT}.sym,1);
    0067     margin = floor((maxSquareSize - squareSize)/2);
    0068     tmpTT{iT}(margin+(1:squareSize),margin+(1:squareSize)) = -single(TransformedTemplate{iT}.sym);
    0069 end
    0070
    0071 im = displayImages(tmpTT,4*2,50,50);
    0072 imwrite(im,'../result/transformedTemplates.png');
    0073
    0074
    0075 %% compute pairwise correlation
    0076 load ../template_params CorrGabor
    0077 subsampleS2 = 5; inhibitionThresS2 = 0;
    0078 save ../result/transformedEllipsoids.mat subsampleS2 inhibitionThresS2 TransformedTemplate EllipseGaborScale subsampleM1
    0079 % CorrS2 = CcomputeCorrS2( nGaborOri, CorrGabor, TransformedTemplate, subsampleS2, inhibitionThresS2);
    0080 % save ../result/transformedEllipsoids.mat CorrS2 subsampleS2 inhibitionThresS2 TransformedTemplate EllipseGaborScale subsampleM1
    0081
    0082 %% Following (obsolete): the matlab code for computing the correlation maps between S2 templates.
    0083 %
    0084 % for iT = 1:nTransformedTemplates
    0085 %     for jT = 1:nTransformedTemplates % floating template
    0086 %         % to do: write a mex-C module to deal with computation of I
    0087 %
    0088 %         I = zeros( 2 * floor( size(TransformedTemplate{iT}.sym,1)/2 ) + 2 * floor( size(TransformedTemplate{jT}.sym,1)/2 ) + 1 );
    0089 %         halfSize = floor(size(I,1)/2);
    0090 %         for row = 1:size(I,1)
    0091 %             for col = 1:size(I,2)
    0092 %
    0093 %                 count = 0;
    0094 %
    0095 %                 for iF = 1:length(TransformedTemplate{iT}.selectedRow)
    0096 %                     % check if there is any overlapping
    0097 %                     is_overlap = 0;
    0098 %                     iOri = TransformedTemplate{iT}.selectedOri(iF);
    0099 %                     iS = TransformedTemplate{iT}.selectedScale(iF);
    0100 %                     iRow = TransformedTemplate{iT}.selectedRow(iF);
    0101 %                     iCol = TransformedTemplate{iT}.selectedCol(iF);
    0102 %                     if iS < 1 || iS > nGaborScale
    0103 %                         continue;
    0104 %                     end
    0105 %                     for jF = 1:length(TransformedTemplate{jT}.selectedRow)
    0106 %                         jOri = TransformedTemplate{jT}.selectedOri(jF);
    0107 %                         jS = TransformedTemplate{jT}.selectedScale(jF);
    0108 %                         jRow = TransformedTemplate{jT}.selectedRow(jF);
    0109 %                         jCol = TransformedTemplate{jT}.selectedCol(jF);
    0110 %                         halfSizeCorrGabor = ( size( CorrGabor{(iS-1)*nGaborOri+iOri, (jS-1)*nGaborOri+jOri}, 1 ) - 1 ) / 2;
    0111 %                         if abs( (row + jRow) - (halfSize + iRow) ) >=...
    0112 %                                 halfSizeCorrGabor || abs( (col + jCol) - (halfSize + iCol) ) >=...
    0113 %                                 halfSizeCorrGabor % no overlap
    0114 %                             continue;
    0115 %                         end
    0116 %                         if CorrGabor{(iS-1)*nGaborOri+iOri, (jS-1)*nGaborOri+jOri}(...
    0117 %                                 (row + jRow) - (halfSize + iRow) + halfSizeCorrGabor, ...
    0118 %                                 (col + jCol) - (halfSize + iCol) + halfSizeCorrGabor ) < 1
    0119 %                             is_overlap = 1;
    0120 %                             break;
    0121 %                         end
    0122 %                     end
    0123 %                     if is_overlap > 0
    0124 %                         count = count + 1;
    0125 %                     end
    0126 %                 end
    0127 %
    0128 %                 if double(count) / double(length(TransformedTemplate{iT}.selectedRow)) < 0.2
    0129 %                     I(row,col) = 1; % 1 means no inhibition
    0130 %                 end
    0131 %             end
    0132 %         end
    0133 %
    0134 %         CorrS2{iT,jT} = I;
    0135 %     end
    0136 % end
    0137
    0138
    0139