function t = thinpks(f,pc,pd)
% t = thinpks(f,pc,pd)
[m n] = size(f);
t = zeros(m,n);
numpks = max(max(f));   % max label number
mins = 100*ones(numpks);   % will keep min distance to peak at best pixel
mini = zeros(numpks);      % keeps best pixel row value
minj = zeros(numpks);      % keeps best pixel col value
for i1 = 1:m
  for j1 = 1:n
    fval = f(i1,j1);
    if fval>0         % there's a peak here
      pcval = pc(i1,j1);   % x offset to peak
      pdval = pd(i1,j1);   % y offset to peak
      dist = sqrt(pcval*pcval+pdval*pdval);  % distance to peak
      if dist <= mins(fval)   
        mins(fval) = dist;       % replace old with new min distance
        mini(fval) = i1;
        minj(fval) = j1;
      end
    end
  end
end
for l = 1:numpks
  if (mini(l)>0) & (minj(l)>0)
    t(mini(l),minj(l)) = f(mini(l),minj(l)); % set thinned image with label no.
  end
end
