function h = TGhillsm(fc,fr,K)
%
% TGhillsl returns non-zero at each pixel with surrounding gradient vectors
%         all within 90 degrees of each other
% On input:
%     fc,fr: gradients of 2d image function
%     K: window dimension
% On output:
%     h: hill labels (value is max angle dif in window)
% Call:
%     h = TGhillsm(fc,fr,K)
%

[m,n] = size(fc);
k = (K+1)/2;
h = zeros(m,n);

theta = atan2(fr,fc);
ptheta = posori(theta);

for r = k:m-k+1
  for c = k:n-k+1
    win = ptheta(r-k+1:r+k-1,c-k+1:c+k-1);
    tmax = max(max(win));
    tmin = min(min(win));
    tdif = tmax-tmin;
    if tdif<pi/2
      h(r,c) = pi-tdif;
    end;
  end;
end;
