function latva=find_trees(name)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function find_trees etsii annetusta tiedosta puiden keskipisteet
% käyttäen konvoluutiota ja 3x3 maksimin etsintää.
% 
% Copyright Juha Hyyppä 5.3.2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      

eval(['load ',name])

[x,y]=size(chm);
latva=zeros(x,y);
chm2=chm;

puut=[];

filt=[1 4 1
      4 16 4
      1 4 1]/36;

chm1=conv2(chm,filt);
for i=2:x-1
    for j=2:y-1
        piste=chm1(i,j);
        A=max(max(chm1(i-1:i+1,j-1:j+1)));
        if abs(piste-A)<0.00001
            %oletettu latvapiste
            latva(i,j)=1;
            chm2(i-1,j-1)=100;
            puut=[puut
                 i j chm(i-1,j-1)];
        end
    end
end    

figure(1)
imagesc(chm)
colorbar
figure(2)
imagesc(chm2)
figure(3)
hist(puut(:,3),20);
pause

  

