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. 
%% parameters for Gabor filters
numScale = 2; 
scales = [.7 1. 1.3 1.6 2.]; 
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
%% parameters for active basis
epsilon = .1; % allowed correlation between selected Gabors 
subsample = 2; % subsample in computing MAX1 maps   
locationShiftLimit = 3; % shift in normal direction = locationShiftLimit*subsample pixels
orientShiftLimit = 1; % shift in orientation
numElement1 = 60; % number of Gabors in active basis at the first scale
%% 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
%% Load in training images
useMinimumSize = 1; resizeFactor = 1.; % resize the input image 
useFixedSize = -1; sizex = 0; sizey = 0; % forced size of input image
imageFolder = 'positiveImage'; % folder of training images  
imageName = dir([imageFolder '/*.jpg']);
numImage = size(imageName, 1); % number of training images 
I = cell(1, numImage); 
for (img = 1 : numImage)
    tmpIm = imread([imageFolder '/' imageName(img).name]); 
    if size(tmpIm,3) == 3
        tmpIm = rgb2gray(tmpIm);
    end
    if (useMinimumSize>0)
       I{img} = imresize(single(tmpIm), resizeFactor, 'nearest');
    end
    if (useFixedSize>0)
       I{img} = imresize(single(tmpIm), [sizex sizey], 'nearest');      
    end     
end
if (useMinimumSize>0)   
    imageSize = zeros(numImage, 2);
    for (img = 1 : numImage)
      imageSize(img, :) = size(I{img});   
    end
    sizex = min(imageSize(:, 1)); sizey = min(imageSize(:, 2)); 
    for (img = 1 : numImage)
      I{img} = I{img}(1:sizex, 1:sizey); 
    end
end    
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', ...
                    '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
thresholdFactor = .01;  % force small responses to zero to avoid flat false positive in detection with window normalization
numResolution = 5;  % 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, nn); 
    for(img=1:numResolution)
       J{img} = imresize(J0, .3+(img-1)*.1, 'nearest');  % scaled images
    end
    testName = ['working/testingimage' num2str(ii)]; 
    save(testName, 'J');  
end
save working/testing nn numResolution inhibitFind thresholdFactor
