% P = find_dog_peaks(DoG, octSize)
%  searches a set of DoG levels for keypoints and returns a 
%  list of such keypoints, scaled (using octSize) back to the
%  positions on the original image. 

function P = find_dog_peaks(DoG, octSize)

Points = [];

PERIM_MARGIN = 5;

%% get parameters
[InitSigma,Scales,PeakThresh,R] = get_dog_params;

for dog_level = 2:size(DoG,3)-1,
  for i=PERIM_MARGIN:size(DoG,1)-PERIM_MARGIN,
    for j=PERIM_MARGIN:size(DoG,2)-PERIM_MARGIN,
      
      % extract a 3x3x3 neighborhood patch from the DOG pyramid
      patch = DoG([i-1:i+1],[j-1:j+1],[dog_level-1:dog_level+1]);

      if ( (patch(2,2,2) > PeakThresh/Scales) &&  ...
	    is_local_extremum(patch) &&  ...
	    not_an_edge(DoG,i,j,R))

	 % it's a keypoint! 
         k=2^(1/Scales);
         sigma_cur = (1/octSize)*InitSigma*k.^(dog_level-1);
 
	 Points = [Points; [i j sigma_cur]];
      end;
      
    end;
    
  end;
    
%% rescale image points back to positions in original image
P = (1/octSize)*Points;
end;