function [Yreg,Yclass] = GrayFeature_ApplyModelToImage(InImage,Model,resolution,ImgRange);
%function [Yreg,Yclass] = GrayFeature_ApplyModelToImage(InImage,Model,resolution,ImgRange);
%
%objects are found that are 4 * the size of resolution

if(length(resolution) == 1), resolution = [resolution,resolution];, end
if(nargin < 4)
  ImgRange = 1;
end

VERBOSE = 1;
if(VERBOSE),tic,end;

if(size(InImage,3) == 3)
  InImage = rgb2gray(InImage);
end

Yreg = [];
Yclass = [];
SIm = im2double(InImage) * ImgRange;
for rowidx = 1:(size(SIm,1) - resolution(1) + 1)
  if(mod(rowidx,10) == 1),
    if(VERBOSE),fprintf('row %d of %d\n',rowidx,(size(SIm,1) - resolution(1) + 1));,end
  end
  X = Windowing_ImageStripToMatrix(SIm,resolution,rowidx);
  if(isSVMModel(Model))
    [yempl,yemp] = CLSosusvmC(X,Model);
  end
  if(isBOOSTModel(Model))
    [yempl,yemp] = CLSgentleBoostC(X,Model);
    yemp = yemp';
    yempl = yempl';
  end
  Yreg = [Yreg;yemp'];
  Yclass = [Yclass;yempl'];
end
if(VERBOSE),fprintf('\nPadding output\n');,end
l = floor(resolution(2)/2);
t = floor(resolution(1)/2);
r = ceil(resolution(2)/2);
b = ceil(resolution(1)/2);
Yreg = padimage(Yreg,[l t b r],'replicate');
Yclass = padimage(Yreg,[l t b r],'replicate');
if(VERBOSE),fprintf('\nDone, total time is \n');,toc,end


function b = isBOOSTModel(Model)
b = 1;
if(~isfield(Model,'Nrounds'))
  b= 0;
end
if(~isfield(Model,'featuresused'))
  b= 0;
end
if(~isfield(Model,'R'))
  b= 0;
end
if(~isfield(Model,'classifier'))
  b= 0;
end

function b = isSVMModel(Model)
b = 1;
if(~isfield(Model,'AlphaY'))
  b= 0;
end
if(~isfield(Model,'SVs'))
  b= 0;
end
if(~isfield(Model,'nSV'))
  b= 0;
end
if(~isfield(Model,'Parameters'))
  b= 0;
end

