%  This program finds the correlation between the two images using
%  straight convolution.
function [M] = cor(S) 
  %  Iterate through the animals.
  for imnum = 1:1:6
    switch imnum
     case{1}
      A = Loadin('myboot.tif');
     case{3}
      A = Loadin('mydeer.tif');
     case{5}
      A = Loadin('mybobcat.tif');
     otherwise
      %  Look for left and right feet (mirror images of each other)
      A = fliplr(A);
    end
    %  Find the self correlation so we have a maximum value on which
    %  to base our measurements.
    self = max(max(xcorr2(A,A)));
    %  To prevent rotation from adding weird errors, make white have
    %  a value of zero in the matrix.
    A = A -.5.*ones(size(A));
    %  Since we are using convolution, prepare the footprint
    %  accordingly
    A = fliplr(flipud(A));
    M = [];
    for a = -90:30:90
      %  Rotate our footprint.
      B = imrotate(A, a, 'crop');
      %  Make white have a value of .5 again
      B = B + .5.*ones(size(B));
      %  Convolve
      C = conv2(S, B, 'same');
      %  Find the peak values of the convolution
      P = peaks(C, self);
      %  If footprints were detected, store them in the output matrix
      if sum(size(P))>0
	if sum(size(M))>0
	  M = [M(1,:), P(1,:); M(2,:), P(2,:); M(3,:), a.* ...
	       ones(size(P,2)); M(4,:), imnum.*ones(size(P,2))];
	else
	  M = [P;a.*ones(size(P,2));imnum.*ones(size(P,2))];
	end
      end
      %  This takes a while so update the user at to progress, or it
      %  becomes very frustrating!
      Angle = a
      Hits = size(P,2)
    end
  end


