function tr = thinr(ridges)
%  tr = thinr(ridges)
[m n] = size(ridges);
tr = ridges;
for i1 = 1:m
  for j1 = 1:n
    if tr(i1,j1)>0
      count = 0;
      iback = max(i1-1,1);
      jback = max(j1-2,1);
      ifor = min(i1+1,m);
      jfor = min(j1+2,n);
      for i2 = iback:ifor
        for j2 = jback:jfor
          if i2~=j2 & tr(i2,j2)>0
            if tr(i2,j2) ~= 16
              count = count+1;
            end
          end
        end
      end
      if count<2
        tr(i1,j1) = 0;
      end
    end
  end
end
