%  Correlation of the input matrix with the footprints using both
%  whitespace and gait optimization.
function [N] = cor3(W, Wmask, S)
  %  Find the first region given by the white space 
    area = 1;
    N = [];
    while area < size(W, 2)
      %  Get the part of the image matrix we want to look at
      slice = S(W(1,area)-16:W(2,area)+16, W(3,area)-16:W(4,area)+16);
      %  Go through all of the prints, both left and right feet
      Pinfo = [];
      for print = 1:1:6
	switch print
	 case{1}
	  A = Loadin('myboot.tif');
	 case{3}
	  A = Loadin('mydeer.tif');
	 case{5}
	  A = Loadin('mybobcat.tif');
	 otherwise
	  A = fliplr(A);
	end
	%  Prepare A for convolution
	A = fliplr(flipud(A));
	%  Find the self correlation
	self = max(max(xcorr2(A,A)));
	%  Prepare A for rotation
	A = A -.5.*ones(size(A));
	%  It would be possible to look at a complete range of
        %  angles, but that would require longer to run, and
        %  provide no addition insights into the problem
	for a = -90:30:90
	  sprintf('Image = %d, Area = %d, Angle = %d', print, area,a)
	  %  Rotate the foot print
	  B = imrotate(A, a, 'crop');
	  %  Put A back into its convolution form
	  B = B+.5.*ones(size(B));
	  %  Do the convolution
	  C = conv2(slice, B, 'valid');
	  P = peaks(C, self);
	  %  Move the peaks to their correct location
	  [pr, pc] = size(P);
	  P = P + [(W(1,area)-17).*ones(pr/2, pc);(W(3,area)-17).* ...
		   ones(pr/2,pc)];
	  %  Find the average x and y coordinates of the points
          %  returned by the peaks.m function.  Also, record the
          %  angle and image that these peaks correspond to.
	  if size(P,1)>0
	    Pcenter = [round(mean(P(1,:))), round(mean(P(2,:)))];
	    if size(Pinfo, 1)>0
	      Pinfo = [Pinfo; Pcenter, size(P,2), print, a];
	    else
	      Pinfo = [Pcenter, size(P,2), print, a];
	    end
	  end
	end
      end
      %  Now we have the Pinfo matrix which contains the
      %  information about the location, angle and animal.  From
      %  this information, we can determine which animal's
      %  footprint was in the region outlined by whitespace.
      if size(Pinfo,1) >0
	%  Find which print was in the region specified by
        %  whitespace by looking at the total number of correlation
        %  peaks corresponding to each animal.  
	[value, index] = max(Pinfo(:,3));
	Pinfo = Pinfo(index, :);
	angle = Pinfo(5)*pi/180;
	centerr = Pinfo(1);
	centerc = Pinfo(2);
	print = Pinfo(4);
	%  Based on the print we know about the tracks
	switch print
	 case{1}
	  stride = 60;
	  straddle = 20;
	  intergroup = 30;
	  color = 'rx';
	 case{3}
	  stride = 120;
	  straddle = 25;	
	  intergroup = 20;
	  color = 'gx';
	 case{5}
	  stride = 90;
	  straddle = 30;	
	  intergroup = 30;
	  color = 'bx';
	 otherwise
	  %  If we found the left foot, then the animal is going in
          %  the other direction.
	  straddle = -straddle;
	  intergroup = -intergroup;
	end
	%  Do some trig to find where we expect the other foot
	%  print to be
	cspan   = stride*cos(angle+pi/2);
	rspan   = -stride*sin(angle+pi/2);
	lrspanc = round(straddle*sin(angle+pi/2)-intergroup*cos(angle+pi/2));
	lrspanr = round(straddle*cos(angle+pi/2)+intergroup*sin(angle+pi/2));
	%  Find the coordinates of the other prints
	r = 0; c=0; rprints=1; cprints=1;step = 1; done = 0;
	while (rprints+lrspanr<size(S, 1))&(rprints>0)&...
	      (cprints+lrspanc<size(S,2))&(cprints>0)
	  rprints = round(r*rspan+centerr);
	  cprints = round(c*cspan+centerc);
	  rlr = rprints+lrspanr;
	  clc = cprints+lrspanc;
	  if r+c == 0
	    prints = [[rprints;cprints],[rprints+lrspanr;cprints+lrspanc]];
	  else
	    prints = [prints, [rprints;cprints], [rprints+lrspanr;cprints+lrspanc]];
	  end
	  %  If we have reached one side of the snowy scene, start
          %  at the original print and go in the other direction.
	  if (rprints<0)|(cprints<0)|(rlr>size(S,1))|(clc>size(S,2))
	    r = 0;
	    c = 0;
	    step = -1;
	    if done < 2
	      rprints = 1;
	      cprints = 1;
	      done = done+1;
	    end
	  end
	  r = r+step;
	  c = c+step;
	end
	%  Make sure that we don't specify a print outside of our
        %  snowy scene.
	[rindex, cindex] = find(prints<0);
	while size(cindex)>0
	  prints = [prints(:,1:(cindex(1)-1)) , ...
		    prints(:,(cindex(1)+1): size(prints, 2))];
	[rindex, cindex] = find(prints<0);
	end
	[rindex, cindex] = find(prints>size(S,1)-17);
	while size(cindex)>0;
	  prints = [prints(:,1:(cindex(1)-1)) , ...
		    prints(:,(cindex(1)+1): size(prints, 2))];
	[rindex, cindex] = find(prints>size(S,1)-32);
	end
	while max(max(prints))>size(S,1)
	  prints = print(:,1:size(prints,2)-32);
	end
	%  Now we know all of the locations were we expect prints
	%  Now determine if there is anything of interest at these
	%  location.  Correlation is not actually preformed.  If a
        %  region of interest was detected at the predicated
        %  location of the print, this is good enough.
	index = 1;
	%  Print out the expected locations.
	plot(prints(2,:), prints(1,:), 'yo')
	pause(.01)
	M = [];
	while(index <= size(prints, 2))
	  cor = mean(mean(Wmask(prints(1,index)+15:prints(1,index)+19, ...
		     prints(2,index)+15:prints(2,index)+19)));
	  if cor>.9
	    Wc1 = find(W(1,:)<prints(1,index)+17);
	    Wc2 = find(W(2,:)>prints(1,index)+17);
	    Wc3 = find(W(3,:)<prints(2,index)+17);
	    Wc4 = find(W(4,:)>prints(2,index)+17);
	    Wc = intersect(intersect(Wc1,Wc2),intersect(Wc3,Wc4));
	    if size(Wc, 1)> 0
	      W = [W(:, 1:Wc-1),W(:, Wc+1:size(W,2))];
	      if size(M,1) == 0
		M = [prints(1,index);prints(2,index);...
		     angle*180/pi;ceil(print/2)];
	      else
		M = [M, [prints(1,index); prints(2,index); ...
			 angle*180/pi;ceil(print/2)]];
	      end
	      index = index - 1;
	    end
	  end
	  index = index + 1;
	end
	%  Plot out the locations were prints were actually found
	if size(M,1)>0
	  plot(M(2, :), M(1,:), color)
	  pause(.01)
	  if size(N,1) ==0
	    N=M;
	  else
	    N=[N,M];
	  end
	end
      end
      area = area+1;
    end











