%SCRIPT_GetMoreAppearanceTrainData_GradientAndGray
%
%intent: extract car and pedestrian appearance data in correspondance with the
%        toyota features extracted previously


HelpTextInfo = help('SCRIPT_GetMoreAppearanceTrainData_GradientAndGrayWavelet');
cd /cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector
load('CAfilelist_FilterSS');%-->CAfilelist
load('/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/DataFractures/SSFracture_Even_01');
%-->DataFracture
TrainIndicies = DataFracture.Train_Obj;
MAXRETURN = 3000;
MAXFRACTIONSIZE = 1000;

load SobelAppearanceTrainData5_car.mat;
%-->ExtractedLocations,N,X,resolution,y
resolution = [128 128];
  
%Get the positive ones.
y = sField2Arr(ExtractedLocations,'y');
CAIdx = sField2Arr(ExtractedLocations,'CAidx');
B = sField2Arr(ExtractedLocations,'bbox',4);
yi = find(y == 1);
B = B(:,yi);
pBBoxs = B';
pBBoxs = floor(pBBoxs);
pIdxs = CAIdx(yi);

[nBBoxs,nIdxs] = GenerateRandNegativeBBoxSet(pBBoxs, pIdxs,'car',10);


load CAFileList_FilterSS;

grayX = [];
gradX = [];
y= [];
previm = -1;
FRACTION_NUM = 1
for i = 1:length(ExtractedLocations)
   imidx = ExtractedLocations(i).CAidx;
   if(imidx ~= previm);
      im = imread(CAfilelist{imidx}.imagename);
      here = pwd;
      cd ~
      [E,C,gradim] = EdisonConfidenceBasedEdgeDetection(im);
      cd(here);
      clear E C;
      previm = imidx;
   end
   gray_cr = imresize(rgb2gray(imcrop_pad(im,ExtractedLocations(i).bbox,'symmetric')),resolution,'bilinear');
   grad_cr = imresize(imcrop_pad(gradim,ExtractedLocations(i).bbox,'symmetric'),resolution,'bilinear');
   grayX(:,end+1) = gray_cr(:);
   gradX(:,end+1) = grad_cr(:);
   y(end+1) = ExtractedLocations(i).y;
   fprintf('.');
   if(mod(i,50)==0)
     fprintf('%d of %d\n',i,length(ExtractedLocations));
     keyboard;
   end
   if(size(grayX,2) == MAXFRACTIONSIZE)
      savename = fullfile(saveroot,sprintf('AppearanceData_128x128_car_f%.3d.mat',FRACTION_NUM));
      save(savename,'grayX','gradX','y');
      FRACTION_NUM = FRACTION_NUM + 1;
      grayX = [];
      gradX = [];
      y = [];
      fprintf('SAVE FRACTION SAVE FRACTION SAVE FRACTION %d\n',FRACTION_NUM-1);
   end
end
