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

EdgeRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/EdgesGradients';
saveroot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector';
HelpTextInfo = help('SCRIPT_GetMoreAppearanceTrainData_GradientAndGrayWavelet');
cd /cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector
load('CAfilelist_FilterSS');%-->CAfilelist
load('PositiveCarBBoxs');%-->pBBoxs, pIdxs;
load('NegativeCarBBoxs');%-->nBBoxs, nIdxs;
%load('PositivePedestrianBBoxs');%-->pBBoxs, pIdxs;
%load('NegativePedestrianBBoxs');%-->nBBoxs, nIdxs;
%load('PositiveBicycleBBoxs');%-->pBBoxs, pIdxs;
%load('NegativeBicycleBBoxs');%-->nBBoxs, nIdxs;


BBoxs = [pBBoxs;nBBoxs];
BBoxs = round(BBoxs);
Idxs = [pIdxs,nIdxs];
Ys = [ones(1,length(pIdxs)),-ones(1,length(nIdxs))];
[i,si] = sort(Idxs);
BBoxs = BBoxs(si,:);
Idxs = Idxs(si);
Ys = Ys(si);
     
MAXFRACTIONSIZE = 1000;
resolution = [128 128];
grayX = [];
gradX = [];
y= [];
myBBox = [];
myIdx = [];
previm = -1;
%FRACTION_NUM = 1
%for i = 1:length(Idxs)
FRACTION_NUM = 9;
for i = 8001:length(Idxs)
   imidx = Idxs(i);
   if(imidx ~= previm);
      im = imread(CAfilelist{imidx}.imagename);
      here = pwd;
      load(fullfile(EdgeRoot,sprintf('EdgeConfGrad_%.5d.mat',imidx)));
      %-->[E,C,G];
      clear E C;
      previm = imidx;
   end
   gray_cr = imresize(rgb2gray(imcrop_pad(im,BBoxs(i,:),'symmetric')),resolution,'bilinear');
   grad_cr = imresize(imcrop_pad(G,BBoxs(i,:),'symmetric'),resolution,'bilinear');
   grayX(:,end+1) = gray_cr(:);
   gradX(:,end+1) = grad_cr(:);
   y(end+1) = Ys(i);
   myBBox((end+1),:) = BBoxs(i,:);
   myIdx((end+1)) = Idxs(i);
   fprintf('.');
   if(mod(i,50)==0)
     fprintf('%d of %d\n',i,length(Idxs));
   end
   if(size(grayX,2) == MAXFRACTIONSIZE)
      %savename = fullfile(saveroot,sprintf('AppearanceData_128x128_bicycle_f%.3d.mat',FRACTION_NUM));
      %savename = fullfile(saveroot,sprintf('AppearanceData_128x128_pedestrian_f%.3d.mat',FRACTION_NUM));
      savename = fullfile(saveroot,sprintf('AppearanceData_128x128_car_f%.3d.mat',FRACTION_NUM));
      toyotaX = GrayMtx2ToyotaMtx(grayX,[128 128]);
      save(savename,'grayX','gradX','toyotaX','y','myBBox','myIdx');
      FRACTION_NUM = FRACTION_NUM + 1;
      grayX = [];
      gradX = [];
      y = [];
      myBBox = [];
      myIdx = [];
      fprintf('SAVE FRACTION SAVE FRACTION SAVE FRACTION %d\n',FRACTION_NUM-1);
   end
end
toyotaX = GrayMtx2ToyotaMtx(grayX,[128 128]);
%savename = fullfile(saveroot,sprintf('AppearanceData_128x128_bicycle_f%.3d.mat',FRACTION_NUM));
%savename = fullfile(saveroot,sprintf('AppearanceData_128x128_pedestrian_f%.3d.mat',FRACTION_NUM));
savename = fullfile(saveroot,sprintf('AppearanceData_128x128_car_f%.3d.mat',FRACTION_NUM));
save(savename,'grayX','gradX','y','myBBox','myIdx','toyotaX');

function [pBBoxs,pIdxs] = PositiveBoundingBoxs(UseIndicies,objname,CAfilelist);
%function [pBBoxs,pIdxs] = PositiveBoundingBoxs(UseIndicies,objname,CAfilelist);
%
pBBoxs = [];
pIdxs = [];
for i = 1:length(UseIndicies)
  CAidx = UseIndicies(i);
  ol = QReadOList2(CAfilelist{CAidx}.olistname);
  if(~isfield(ol,objname)), continue, end;
  for j = 1:length(ol.(objname))
    bbox_orig = poly2bbox(ol.(objname){j});
    bbox = bboxEnforceAspectRatio(bbox_orig,[.1 .1],'bottom');
    %for padding around the crop.
    bbox = [bbox(1) - bbox(3)/6,bbox(2) - bbox(4)/6,bbox(3)*4/3,bbox(4)*4/3];
    if(BBoxIsInside(bbox,[.5 .5 960 1280]));
      pBBoxs(end+1,:) = bbox;
      pIdxs(end+1) = CAidx;
    end
  end
end  


