clear all; close all; 
disp(['===============> Getting parameters, mex-c code, and training and testing images']);
mkdir('working'); % working directory to store .mat files. 
mkdir('output'); % directory that contains output from the code. 
%% mex-C code
mex Chistogram.c;   % pool histogram from negative images
mex CsharedSketch.c; % learning by shared sketch algorithm
mex Csigmoid.c; % sigmoid transformation
mex CgetMAX1track.c; % get MAX1 maps and track maps
mex Caverage.c; % sigmoid transformation
mex CgetSUM2.c;   % template matching inference
mex CgetMAX2.c;   % template matching inference
mex CgetBack.c;   % template matching inference
mex CgetBackInhibit.c;   % template matching with inhibition 
mex ClocalNormalizeDouble.c; % local normalization of type double
mex ClocalNormalize.c; % local normalization
%% parameters for Gabor filters
numScale = 1; 
scales = [.7]; 
numOrient = 15;  % number of orientations
saturation = 6.; % saturation level for sigmoid transformation
%% parameter for exponential model
binSize = .2;  % binsize for computing histogram of q()
numStoredPoint = 50; % number of stored lambda values
spacing = .1; % spacing between two adjacent points
%% parameters for normalization
localOrNot = 1; % if we use local normalization or not. If not, set it to -1 
localHalfx1 = 10; localHalfy1 = 10; % the half range for local normalization, has to be quite large
windowNormalizeOrNot = -1; % whether normalize within the scanning window in detection 
if (localOrNot>0)
    windowNormalizeOrNot = -1; 
end % if we use local normalization, we should not use window normalization in detection
thresholdFactor = .05;  % divide the response by max(average, maxAverage*thresholdFactor)
%% parameters for active basis
epsilon = .1; % allowed correlation between selected Gabors 
subsample = 1; % subsample in computing MAX1 maps   
locationShiftLimit = 3; % shift in normal direction = locationShiftLimit*subsample pixels
orientShiftLimit = 1; % shift in orientation
numElement1 = 20; % number of Gabors in active basis at the first scale
%% Load in training images
numImage = 1; % number of training images 
I = cell(1, numImage);
sizex = 99; sizey = 99;
center = 50; a = 30; b = 10
I0 = single(zeros(sizex, sizey)); 
for (x = 1:sizex)
        for (y = 1:sizey)
            if (((x-center)/a)^2+((y-center)/b)^2<1.)
                I0(x, y) = single(1.); 
            end
        end
end
I{1} = I0+0.;                 
save working/trainingImage I numImage sizex sizey;  
%% save parameters
sizeTemplatex = sizex; sizeTemplatey = sizey; 
templateHalfx = floor(sizex/2.); templateHalfy = floor(sizey/2.); 
parameterName = ['working/parameter'];
save(parameterName, 'numScale', 'scales', 'numOrient', 'saturation', ...
                    'binSize', 'numStoredPoint', 'spacing', ...
                    'localOrNot', 'localHalfx1', 'localHalfy1', 'windowNormalizeOrNot', 'thresholdFactor', ...
                    'epsilon', 'subsample', 'locationShiftLimit', 'orientShiftLimit', 'numElement1', ...
                    'sizeTemplatex', 'sizeTemplatey', 'templateHalfx', 'templateHalfy'); 
%% load in testing image, generate multiple resolutions
inhibitFind = -1;  % whether to apply inhibition after detection for re-computing MAX2 score
numResolution = 10;  % total number of resolutions to search 
imageFolder = 'Testing'; % folder of training images  
imageName = dir([imageFolder '/*.jpg']);
nn = size(imageName, 1); % number of training images
for (ii = 1 : nn)
    tmpIm = imread([imageFolder '/' imageName(ii).name]); 
    if size(tmpIm,3) == 3
        tmpIm = rgb2gray(tmpIm);
    end
    J0 = single(tmpIm); 
    J = cell(1, numResolution); 
    for(img=1:numResolution)
       J{img} = imresize(J0, .1+(img-1)*.02, 'nearest');  % scaled images
    end
    testName = ['working/testingimage' num2str(ii)]; 
    save(testName, 'J');  
end
save working/testing nn numResolution inhibitFind 

