function [Npf,spx,spy]=findpeaks(img,mk,GoodBallsCutOff,mnApp)

[NNx NNy]=size(img);

uu=mk;
for n=-1:1
  for m=-1:1
    if(~(m==0 & n==0)) 
      uu=(img>img(rem((1:NNx)+NNx+n-1,NNx)+1,rem((1:NNy)+NNy+m-1,NNy)+1)) & uu;
    end
  end
end

g1=find((img(:).*uu(:))>GoodBallsCutOff);
Npf=length(g1);
[spx spy]=ind2sub([NNx NNy],g1);
[junk iii]=sort(img(g1));
Xn=repmat(spx(iii)+i*spy(iii),1,Npf);
dd=abs(Xn.'-Xn);
iix=iii(find(sum(tril(dd<mnApp & dd~=0))));
g1=g1(setdiff(1:Npf,iix));
[junk iii]=sort(img(g1),1,'descend');
Npf=length(g1);
[spx spy]=ind2sub([NNx NNy],g1(iii));


