# ConstructEgretShapeScript

## PURPOSE

ConstructEgretShapeScript - Construct egret shape script template.

## SYNOPSIS

This is a script file.

## DESCRIPTION

     ConstructEgretShapeScript - Construct egret shape script template.

       The shape script contains an ellipsoid, an angle, and two pairs of parallel bars.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- StartFromHere StartFromHere - The entry point of this project.

## SOURCE CODE

    0001 % ConstructEgretShapeScript - Construct egret shape script template.
    0002 %
    0003 %   The shape script contains an ellipsoid, an angle, and two pairs of parallel bars.
    0004
    0005 clear
    0006 close all
    0007 addpath('common');
    0008
    0009 % lesson learned:
    0010 %  it is easier to watch the S1map, M1Map, S2Map, M2map when debugging.
    0011
    0012 %% specify an S3 template
    0013 % Firstly specify the absolute positions of ellispoids, then transform them
    0014 %   to positions on the subsampled lattice.
    0015
    0016 % load the S2 dictionary
    0017 load template_params
    0018 load result/transformedS2Template.mat
    0019
    0020 subsampleS2 = 3;
    0021 selectedRow = single([0 -45 -40 -20]);
    0022 selectedCol = single([0 -50 -30 -30]);
    0023 selectedTransform = single([114 7 18 18]-1);
    0024 selectedS2Ind = single([0 1 2 2]);
    0025 % selectedRow = 0;
    0026 % selectedCol = 0;
    0027 % selectedTransform = selectedTransform(2);
    0028 % selectedS2Ind = selectedS2Ind(2);
    0029 nF = length(selectedRow);
    0030 top = min(selectedRow);
    0031 bottom = max(selectedRow);
    0032 left = min(selectedCol);
    0033 right = max(selectedCol);
    0034 S3size = max(bottom-top,right-left);
    0035 selectedLambda = 3 * single(ones(1,nF));
    0036 selectedLogZ = single(4 * ones(1,nF));
    0037
    0038
    0039 %% display the S3 template
    0040 % TODO: When drawing the S3 template, re-center the features first. Do the same for S2 templates.
    0041 margin = 60;
    0042 height = bottom - top + 1 + 2 * margin;
    0043 width = right - left + 1 + 2 * margin;
    0044 canvas = zeros(height,width);
    0045 for iF = 1:nF
    0046     iT = selectedTransform(iF)+1;
    0047     iS2 = selectedS2Ind(iF) + 1;
    0048     patch = TransformedTemplate{iT,iS2}.sym;
    0049     row = selectedRow(iF) + floor(height/2);
    0050     col = selectedCol(iF) + floor(width/2);
    0051     %
    0052     % place the patch to (row,col)
    0053     %
    0054     % below: the bounding box inside the large canvas
    0055     top2 = row - floor(size(patch,1)/2);
    0056     left2 = col - floor(size(patch,2)/2);
    0057     bottom2 = top2 + size(patch,1) - 1;
    0058     right2 = left2 + size(patch,2) - 1;
    0059     %
    0060     for c = 1:size(patch,2)
    0061         for r = 1:size(patch,1)
    0062             val = patch(r,c);
    0063             if val > canvas(top2+r-1,left2+c-1)
    0064                 canvas(top2+r-1,left2+c-1) = val;
    0065             end
    0066         end
    0067     end
    0068 end
    0069 canvas([1:2 end-1:end],:) = max(canvas(:))/2;
    0070 canvas(:,[1:2 end-1:end]) = max(canvas(:))/2;
    0071
    0072
    0073 category = 'egret';
    0074 h=figure; imshow(-canvas,[]);
    0075 mkdir(sprintf('result/%s',category));
    0076 saveas(h,sprintf('result/%s/template.png',category));
    0077 saveas(h,sprintf('result/%s/%s_template.eps',category,category), 'epsc');
    0078 close
    0079 S3Template = cell(1,1);
    0080 S3Template{1} = struct('selectedRow',floor(selectedRow/subsampleS2),'selectedCol',floor(selectedCol/subsampleS2),...
    0081     'selectedInd',selectedS2Ind,'selectedTransform',selectedTransform,'selectedLambda',selectedLambda...
    0082     ,'selectedLogZ',selectedLogZ);
    0083
    0084
    0085 %% Compute an adjacency matrix of S2 templates (used in computing M2 maps)
    0086 nTransform = size(TransformedTemplate,1);
    0087 nS2 = size(TransformedTemplate,2);
    0088 templateAffMat = cell(nTransform,1);
    0089 for iT = 1:nTransform
    0090     tmp = templateTransform{iT};
    0091     s1 = tmp(1); rs1 = tmp(2); cs1 = tmp(3); r1 = tmp(4);
    0092     neighbors = [];
    0093     for jT = 1:nTransform
    0094         tmp = templateTransform{jT};
    0095         s2 = tmp(1); rs2 = tmp(2); cs2 = tmp(3); r2 = tmp(4);
    0096         alpha1 = single(r1)/nGaborOri*pi ;
    0097         alpha2 = single(r2)/nGaborOri*pi ;
    0098         diff_alpha = alpha1 - alpha2;
    0099         while diff_alpha < 0
    0100             diff_alpha = diff_alpha + pi;
    0101         end
    0102         while diff_alpha >= pi
    0103             diff_alpha = diff_alpha - pi;
    0104         end
    0105         if diff_alpha > pi/2
    0106             diff_alpha = pi - diff_alpha;
    0107         end
    0108         if abs(s2 - s1) <= 0 ...
    0109                 && diff_alpha < pi/3;
    0110             neighbors = [neighbors jT];
    0111         end
    0112     end
    0113     templateAffMat{iT} = int32(neighbors-1);
    0114 end
    0115
    0116 % save
    0117 save(sprintf('result/%s/shapescript.mat',category),'templateAffMat','S3Template','nTransform','nS2','nF','S3size', 'subsampleS2');