function A=length_elegans(start,finish)
%computation of the actual length of the animal
%   COPYRIGHT:  George D. Tsibidis(1) and Nektarios Tavernarakis(2)
%               (1) Institute of Electronic Structure and Laser 
%               (2) Institute of Molecular Biology and Biotechnology 
%               Foundation for Research and Technology-Hellas (FORTH)
%               January 2007
%

  cd ('DATA_SAVED')
for i=start:finish
length_i=i
  
    A=load(['skeleton_' int2str(i)]);
   % figure;imshow(A)
 

    B=load(['edge_' int2str(i) '.txt']);
    %starting point
    x=B(2,1);  %column
    y=B(1,1);  %row 
    number_of_whites=length(find(A==1));
    % start computing length ad removing white pixels
    leng=[];
   
    for k=1:number_of_whites-1
       % k=k
        %3x3 element
        C=zeros(size(A,1),size(A,2));
         C(y-1,x-1)=1;
         C(y-1,x)=1;
         C(y-1,x+1)=1;
         C(y,x-1)=1;
         C(y,x)=1;
         C(y,x+1)=1;
         C(y+1,x-1)=1;
         C(y+1,x)=1;
         C(y+1,x+1)=1;
         C_new=A.*C;C_new(y,x)=0;
         A(y,x)=0;
         %if k>=86
         %figure;imshow(C_new)
         %end
         [r,c]=find(C_new==1);
         leng1=sqrt((y-r)^2+(x-c)^2);
       leng=[leng,leng1]  ;
       y=r;
       x=c;
     
       
         
   % figure;imshow(A);
    
    %figure;imshow(C);
        
     %   figure;imshow(A.*C)
        
       
            
            
        
    end
    
   leng=sum(leng);
    aq=['length_' int2str(i) '.txt'];
    save(aq,'leng','-ascii')  ;
    
  
    
    
    
end

cd ..