function PROCEDURE_CollectContextAtAppearanceLocations(objname)
%
%intent:  Rigid object detection was performed using crops
% if we are to show contextual improvement we must sample the context at these locations

ContextRoot = '/cbcl/scratch01/bileschi/TextureRec/ContextClassification/';
saveroot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector';
HelpTextInfo = help('SCRIPT_GetMoreAppearanceTrainData_GradientAndGrayWavelet');
cd /cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector
load('CAfilelist_FilterSS');%-->CAfilelist

if(nargin < 1)
  objname = 'bicycle';
end

switch lower(objname)
  case 'bicycle'
    load('PositiveBicycleBBoxs');%-->pBBoxs, pIdxs;
    load('NegativeBicycleBBoxs');%-->nBBoxs, nIdxs;
  case 'car'
    load('PositiveCarBBoxs');%-->pBBoxs, pIdxs;
    load('NegativeCarBBoxs');%-->nBBoxs, nIdxs;
  case 'pedestrian'
    load('PositivePedestrianBBoxs');%-->pBBoxs, pIdxs;
    load('NegativePedestrianBBoxs');%-->nBBoxs, nIdxs;
end


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;

ctx_C2 = [];
ctx_C2X = [];
ctx_Tru = [];
ctx_C2Tru = [];
ctx_BWTru = [];
ctx_X = [];
ctx_C2XNoPos = [];
ctx_TruNoPos = [];
y = [];
previm = -1;

LegendNames{1} = 'C2';
LegendNames{2} = 'C2Tru';
LegendNames{3} = 'Tru';
LegendNames{4} = 'BWTru';
LegendNames{5} = 'C2X';
LegendNames{6} = 'X';
LegendNames{7} = 'TruNoPos';
LegendNames{8} = 'C2XNoPos';


FRACTION_NUM = 1
for i = 1:length(Idxs)
   imidx = Idxs(i);
   if(imidx > 300), continue, end;
   if(imidx ~= previm);
      im = imread(CAfilelist{imidx}.imagename);
  %    TruNoPos = load(fullfile(ContextRoot,'TruNoPos',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> TruNoPos.CtxYemp, TruNoPos.CtxYempl;
  %    C2XNoPos = load(fullfile(ContextRoot,'C2XNoPos',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> C2XNoPos.CtxYemp, C2XNoPos.CtxYempl;
      X = load(fullfile(ContextRoot,'X',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> X.CtxYemp, X.CtxYempl;
      C2 = load(fullfile(ContextRoot,'C2',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> C2.CtxYemp, C2.CtxYempl;
  %    C2X = load(fullfile(ContextRoot,'C2X',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> C2X.CtxYemp, C2X.CtxYempl;
  %    Tru = load(fullfile(ContextRoot,'Tru',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> Tru.CtxYemp, Tru.CtxYempl;
      C2Tru = load(fullfile(ContextRoot,'C2Tru',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> C2Tru.CtxYemp, C2Tru.CtxYempl;
  %    BWTru = load(fullfile(ContextRoot,'BWTru',objname,sprintf('CtxMap_%s_%.5d.mat',objname,imidx)));
      %--> BWTru.CtxYemp, BWTru.CtxYempl;
      previm = imidx;
   end
   bbc_xy = [(BBoxs(i,1) + BBoxs(i,3)/2),(BBoxs(i,2) + BBoxs(i,4)/2)];
   bbc_xy = round(bbc_xy /16);
   ctx_C2(end+1) = C2.CtxYemp(bbc_xy(2),bbc_xy(1));
  % ctx_C2X(end+1) = C2X.CtxYemp(bbc_xy(2),bbc_xy(1));
  % ctx_Tru(end+1) = Tru.CtxYemp(bbc_xy(2),bbc_xy(1));
   ctx_C2Tru(end+1) = C2Tru.CtxYemp(bbc_xy(2),bbc_xy(1));
  % ctx_BWTru(end+1) = BWTru.CtxYemp(bbc_xy(2),bbc_xy(1));
  % ctx_C2XNoPos(end+1) = C2XNoPos.CtxYemp(bbc_xy(2),bbc_xy(1));
   ctx_X(end+1) = X.CtxYemp(bbc_xy(2),bbc_xy(1));
  % ctx_TruNoPos(end+1) = TruNoPos.CtxYemp(bbc_xy(2),bbc_xy(1));
   y(end+1) = Ys(i);
   fprintf('.');
   if(mod(i,50)==0)
     fprintf('%d of %d\n',i,length(Idxs));
   end
   if(size( ctx_C2,2) == MAXFRACTIONSIZE)
   %   savename = fullfile(saveroot,sprintf('ContextData_128x128_%s_f%.3d.mat',objname,FRACTION_NUM));
   %   save(savename,'ctx_X','ctx_TruNoPos','ctx_C2XNoPos','ctx_C2','ctx_C2X','ctx_Tru','ctx_C2Tru','ctx_BWTru','y');
      savename = fullfile(saveroot,sprintf('ContextData_24sample_%s_f%.3d.mat',objname,FRACTION_NUM));
      save(savename,'ctx_X','ctx_C2','ctx_C2Tru','y');
      FRACTION_NUM = FRACTION_NUM + 1;
      ctx_C2 = [];
      ctx_C2X = [];
      ctx_Tru = [];
      ctx_C2Tru = [];
      ctx_BWTru = [];
      ctx_X = [];
      ctx_C2XNoPos = [];
      ctx_TruNoPos = [];
      y = [];
    fprintf('SAVE FRACTION SAVE FRACTION SAVE FRACTION %d\n',FRACTION_NUM-1);
   end
end
savename = fullfile(saveroot,sprintf('ContextData_24sample_%s_f%.3d.mat',objname,FRACTION_NUM));
save(savename,'ctx_X','ctx_C2','ctx_C2Tru','y');
% savename = fullfile(saveroot,sprintf('ContextData_128x128_%s_f%.3d.mat',objname,FRACTION_NUM));
% save(savename,'ctx_X','ctx_TruNoPos','ctx_C2XNoPos','ctx_C2','ctx_C2X','ctx_Tru','ctx_C2Tru','ctx_BWTru','y');
 

