function tr = thinr(ridges,K)
%  tr = thinr(ridges,K)
%      Eliminate ridge segments that don't have K horizontal neighbors
[m n] = size(ridges);
tr = ridges;
for i1 = 1:m
  for j1 = 1:n
    if tr(i1,j1)>0
      count = 0;
      jback = j1-1;
      stop = 0;
      while stop == 0 & jback>0 & count<K
        if tr(i1,jback) == 0
          stop = 1;
        else
          count = count+1;
          jback = jback-1;
        end
      end
      jfor = j1+1;
      stop = 0;
      while stop ==0 & jfor<=n & count< K
        if tr(i1,jfor) == 0
          stop = 1;
        else
          count = count+1;
          jfor = jfor+1;
        end
      end
      if count<K
        tr(i1,j1) = 0;
      end
    end
  end
end
