%SCRIPT_BrainFeaturesAppearanceData
%
%intent: add brian features to the data in d = dir(fullfile(Root,sprintf('AppearanceData_128x128_%s*.mat',on)));
%  use the first third of the data for feature prep

objname = 'car';
Root = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector';
TempRoot = '/cbcl/scratch01/bileschi/Temp';


%(1) load the database
d = dir(fullfile(Root, sprintf('AppearanceData_128x128_%s_*.mat',objname)));
FullGrayX  = [];
n = 0;
for dbnum = 1:length(d)
   DBFile = fullfile(Root, d(dbnum).name);
   load(DBFile);%--> grayX, etc. 
   %(2) re-record the database to a temp directory
   %    and create the data structure for brian features
   nImg = size(grayX,2);
   for i = 1:nImg
    n = n+1;
    savename = fullfile(TempRoot,sprintf('temp_%s_%.5d.bmp',objname,n));
    imwrite(reshape(grayX(:,i)/256,[128 128]), savename, 'bmp');
    sI(n).FullName = savename;
    sI(n).BBox = [];
    sI(n).y = y(i);
   end
end
save(fullfile(TempRoot,sprintf('temp_%s_sI.mat',objname)),'sI');

%(3) Brian feature prep
L = length(sI);
TrainMax = ceil(L / 3);
ally = sField2Arr(sI,'y');
BrianPatchIdxs = intersect(1:TrainMax,find(ally == 1));
PrepBrianParams.WinSizeX = 70;
PrepBrianParams.WinSizeY = 70;
PrepBrianParams.MINPATCHSIZE = 4;
PrepBrianParams.MAXPATCHSIZE = 20;
PrepBrianParams.NUMPATCHSAMPLES = 400;
PrepBrianParams.JITTERSIZE = 5;
PrepBrianParams.PVAL = 5;
PrepBrianParams.method = 'bilinear';
BrianParams = COMPUTEFEATUREPREP_BRIAN(sI(1:TrainMax),PrepBrianParams)
save(fullfile(TempRoot,sprintf('temp_%s_BrianParams.mat',objname)),'BrianParams');
%(4) Calculate brian features for all images
X = COMPUTEFEATURE_BRIAN(sI,BrianParams)
save(fullfile(TempRoot,sprintf('temp_%s_BrianFeats.mat',objname)),'X');
%(5) Add brian features to the original database.




