function c1params = PROCEDURE_ShapeBasedObjectsToC1(Root,Template, NewTemplate, sourceIdx, variablename, objidx,options);
%function c1params = PROCEDURE_ShapeBasedObjectsToC1(Root,Template, NewTemplate, sourceIdx, variablename, objidx,options);
%
%read the grayscale data and convert it to C1
which C1
HelpTextInfo = 'GetPerformance';
d.includeborder = 1;
d.includelaplacianfilter = 0;
d.includedoubleorientations = 0;
d.includegrayscalefilter = 0;
d.smallerscalesonly = 0;
d.toyotalikefilter = 0;

if(nargin < 7)
 options = [];
end
options = ResolveMissingOptions(options,d);
objname{1} = 'car';
objname{2} = 'pedestrian';
objname{3} = 'bicycle';

on = objname{objidx};
[y, X] = GetNewDataX(Template, variablename, sourceIdx, on,Root);
[C1X,c1params] = GrayX2C1X(X,[128 128],options);
savename = fullfile(Root,sprintf(NewTemplate, on,sourceIdx));
save(savename,'C1X','y','HelpTextInfo','c1params','options');


function [yfull, NewDataX] = GetNewDataX(Template, variablename,sourceIdx,on,Root)
NewDataX = [];
yfull = [];
iFl = sourceIdx;
D = load(fullfile(Root,sprintf(Template,on,sourceIdx)));%-->grayX,toyotaX,y;
NewDataX = [NewDataX,getfield(D,variablename)];
yfull = [yfull,D.y]; 


function [C1X,c1params] = GrayX2C1X(X,originalres,options);
if(options.includedoubleorientations)
  rot = [-67.5:22.5:90];
else
  rot = [90 -45 0 45];
end
if(options.smallerscalesonly)
  RF_siz    = [3:2:23];
  div = [4.1:-.05:3.60];
  c1ScaleSS = [1:2:10,11];
  c1SpaceSS = [6:2:14];
else
  c1ScaleSS = [1:2:18];
  RF_siz    = [7:2:39];
  c1SpaceSS = [8:2:22];
  div = [4:-.05:3.2];
end
if(options.toyotalikefilter)
  rot = [90 -45 0 45];
  RF_siz = [3,3];
  div = [4,4];
  c1ScaleSS = [1:2:3];
  c1SpaceSS = [24];
  options.includelaplacianfilter = 1;
  options.includegrayscalefilter = 1;
  c1OL = 3; 
  fSiz = repmat(3,[1,12]);
  filters(:,1) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,2) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,3) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,4) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,5) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,6) = [0;0;0;0;1;0;0;0;0];
  filters(:,7) = [1;1;1;0;0;0;-1;-1;-1];
  filters(:,8) = [-1;0;1;-1;0;1;-1;0;1];
  filters(:,9) = [0;1;1;-1;0;1;-1;-1;0];
  filters(:,10) = [1;1;0;1;0;-1;0;-1;-1];
  filters(:,11) = [-1;-1;-1;-1;8;-1;-1;-1;-1];
  filters(:,12) = [0;0;0;0;1;0;0;0;0];
  numSimpleFilters = 6;
else
  [fSiz,filters,c1OL,numSimpleFilters] = init_gabor_alpha(rot, RF_siz, div,options);
end

for i = 1:size(X,2)
  fprintf('Converting Grayscale To C1 %.4d of %.4d\r',i,size(X,2));
  stim = reshape(X(:,i),originalres);
  %[c1,s1] = C1(stim, filters, fSiz, c1SpaceSS, c1ScaleSS, c1OL,options.includeborder, [1 1 1 1 1 0]);
  [c1,s1] = C1(stim, filters, fSiz, c1SpaceSS, c1ScaleSS, c1OL,options.includeborder);
  c1v = vectorizeCellArray(c1);
  if (i == 1)
    C1X = zeros(length(c1v),size(X,2));
  end
  C1X(:,i) = c1v;
end
fprintf('\n done. \n');

c1params = [];
c1params.rot = rot;
c1params.div = div;
c1params.RF_siz = RF_siz;
c1params.c1ScaleSS = c1ScaleSS;
c1params.c1SpaceSS = c1SpaceSS;


