function DL = PeakDetection_LocalSuppression(caLyr,StopConditions)
%supression takes place in the Lyr variable
%so that multiple scales can be suppressed individually
%
%input is a cell array of 3d arrays
%each cell represents an independant classifier
%each layer represents a scale.
Default.max_n_return = 25;
Default.min_return_val = -inf;
if(exist('StopConditions'))
  StopConditions = ResolveMissingOptions(StopConditions,Default);
else
  StopConditions = Default;
end
nReturnSoFar = 0;
previousReturnVal = -inf;
imsiz = size(caLyr{1});
while ~((nReturnSoFar == StopConditions.max_n_return) || ...
   (previousReturnVal < StopConditions.min_return_val))
   nReturnSoFar = nReturnSoFar + 1;
   i = nReturnSoFar;
   for iClassifier = 1:length(caLyr)
      MaxImgs(:,:,iClassifier) = CombineToyotaDetectionOutputs(caLyr{iClassifier});
   end
   [DetectionList(i).str, DetectionList(i).loc] = max2d(sum(MaxImgs,3));
   z = DetectionList(i).loc;
   DetectionList(i).rel_loc_xy = CoordReal2Relative(z([2,1]),imsiz(1:2));
   DetectionList(i).classifier = find( MaxImgs(z(1),z(2),:)==max(MaxImgs(z(1),z(2),:)) );
   DetectionList(i).classifier(1);
   c = DetectionList(i).classifier;
   DetectionList(i).scale = find( caLyr{c}(z(1),z(2),:)==max(caLyr{c}(z(1),z(2),:)) );
   DetectionList(i).scale = DetectionList(i).scale(1);
   for iClassifier = 1:length(caLyr)
      [caLyr{iClassifier}] = LocalSupression(DetectionList(i),caLyr{iClassifier});
   end
   previousReturnVal = DetectionList(i).str;
end
DL = DetectionList;
if(previousReturnVal < StopConditions.min_return_val)
  DL = DL(1:(end-1));
end



function [Lyr] = LocalSupression(Detection,Lyr);
%dampen response with gaussian in Lyr
nScal = size(Lyr,3);
m = min(min(min(Lyr)));
Lyr = Lyr - m;
Supr = zeros(size(Lyr));
DetScale = 2 ^ ((Detection.scale - 1) / 4);
SuprLyr = GausImg([size(Lyr,1),size(Lyr,2)],Detection.loc, DetScale*16*2);
SuprLyr = SuprLyr;
ScaleWeight = exp(-(((1:nScal) - Detection.scale).^2)/2);
for i = 1:nScal
  Lyr(:,:,i) = Lyr(:,:,i) .* (1 - SuprLyr * ScaleWeight(i));
end
Lyr = Lyr + m;

