function c = crosscor(a,b)
%
% crosscor: normalized cross correlation of mask with image
%           (see Jain,Kasturi and Schunk, p.483)
% On input:
%     a: 2D image
%     b: 2D mask
% On output:
%     c: normalized cross correlation
% Call:
%     c = crosscor(a,b)
%

[ma,na] = size(a);
[mb,nb] = size(b);
wm = (mb-1)/2;
wn = (nb-1)/2;
c = zeros(ma,na);

for i1 = 1+wm:ma-wm
  for j1 = 1+wn:na-wn
    win = a(i1-wm:i1+wm,j1-wn:j1+wn);
    windenom = sqrt(sum(sum(win.*win)));
    if windenom ~= 0
      c(i1,j1) = sum(sum(win.*b))/windenom;
    end
  end
end

%c = max(max(abs(1-c)))-abs(1-c);
