function labels = TGTS2(f)
%
% TGTS2: Tom's Gradient Topographic Sketch: uses directions only
%    labels = TGTS2(f)

[m,n] = size(f);
[fc,fr] = gradient(f);
gdir = eightdir(fc,fr);   % convert gradient vector into 8 neighbor dirs
labels = zeros(m,n);
for r = 3:m-2
  for c = 3:n-2
%  r
%  c
    label = 0;
    p1 = gdir(r,c+2);
    p2 = gdir(r-1,c+2);
    p3 = gdir(r-2,c+2);
    p4 = gdir(r-2,c+1);
    p5 = gdir(r-2,c);
    p6 = gdir(r-2,c-1);
    p7 = gdir(r-2,c-2);
    p8 = gdir(r-1,c-2);
    p9 = gdir(r,c-2);
    p10 = gdir(r+1,c-2);
    p11 = gdir(r+2,c-2);
    p12 = gdir(r+2,c-1);
    p13 = gdir(r+2,c);
    p14 = gdir(r+2,c+1);
    p15 = gdir(r+2,c+2);
    p16 = gdir(r+1,c+2);
    if ((p1==4)|(p1==5)|(p1==3)) & ((p2==4)|(p2==5)|(p2==3)) ...
     & ((p16==4)|(p16==5)|(p16==3)) & ((p8==0)|(p8==1)|(p8==7)) ...
     & ((p9==0)|(p9==1)|(p9==7)) & ((p10==0)|(p10==1)|(p10==7))
      label = 1;
    end
    if ((p4==5)|(p4==6)|(p4==7)) & ((p5==5)|(p5==6)|(p5==7)) ...
     & ((p6==5)|(p6==6)|(p6==7)) & ((p12==1)|(p12==2)|(p12==3)) ...
     & ((p13==1)|(p13==2)|(p13==3)) & ((p4==1)|(p4==2)|(p4==3))
      label = 1;
    end
    if ((p2==4)|(p2==5)|(p2==6)) & ((p3==4)|(p3==5)|(p3==6)) ...
     & ((p4==4)|(p4==5)|(p4==6)) & ((p10==0)|(p10==1)|(p10==2)) ...
     & ((p11==0)|(p11==1)|(p11==2)) & ((p12==0)|(p12==1)|(p12==2))
      label = 1;
    end
    if ((p14==2)|(p14==3)|(p14==4)) & ((p15==2)|(p15==3)|(p16==4)) ...
     & ((p16==2)|(p16==3)|(p16==4)) & ((p6==0)|(p6==7)|(p6==6)) ...
     & ((p7==0)|(p7==7)|(p7==6)) & ((p8==0)|(p8==7)|(p8==6))
      label = 1;
    end
    labels(r,c) = label;
  end
end
