%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  File: trainDetector.m
%  Matlab script file
%  Date: Oct, 08
%
% Use the PCA model for eye images trained in svdEyes to detect eyes.
% Both out-of-subspace and within-subspace statistics are considered.

% Dependencies, Toolboxes:
%      iseToolbox/
%      utvisToolbox/file/
% Data files: trainSet.mat testSet.mat svdEyes.mat 
% To get svdEyes.mat you need to run the script file 
% trainEigenEyes.m and do the save near the end of that file.

% Author: YOU
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% Check Path and Constants  %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%close all;

BATCH = false;

if ~exist('rescaleImageVectors','file')
  addpath('./util');
end

%  Check path.  If these aren't on your path, then you will need
%  to add these toolboxes to your path.  See ~jepson/pub/matlab/startup.m
which showIm          % should be iseToolbox\pyrTools\showIm.m

%%%%%%%%  Initialize random number generator %%%%%%%%%%%%%%%%%%%%%%%
% Random number generator seed:
seed = round(sum(1000*clock));
rand('state', seed);
seed0 = seed;

%%%%%%%%%%% Load svd results along with eye and non-eye images %%%%%%%%%%%%%
load('svdEyes');
sigmaTarg = sigmaUniEye;

load('trainSet');
nTarg = size(eyeIm,2);
nNon = size(nonIm, 2);
testImSz = sizeIm;

%% %%%%%%%%%
% Do brightness normalization
% %%%%%%%%%%
respEye = rescaleImageVectors(eyeIm);
respNon = rescaleImageVectors(nonIm);


% Subtract the mean image 
testTarg =  respEye - repmat(meanIm, 1, nTarg);
testNon = respNon - repmat(meanIm, 1, nNon);


norm2Targ = sum(testTarg.^2,1);
norm2Non = sum(testNon.^2, 1);


%%%%%%%%%% Statistics Plots and ROC curves %%%%%%%%%%%%%%%%%%%%%%%%%
trial_nBasis = [1 5 10 20 50];
USE_VAR_IM = true;

col = hsv(length(trial_nBasis));

% Figure 1 will contain ROC plots, with a legend.
% hCurve and legendNames is useful for curve handles, and legend labels.
figure(1); 
%clf;
hCurve = [];
legendNames = [];
 

for kTrial = 1:length(trial_nBasis)
  nBasis = trial_nBasis(kTrial);
  legendNames = [legendNames; sprintf('nBasis =%3d', nBasis)];
  

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% End: trainDetector %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
