function a=point_segment(N,a,b)
%%   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
%
%
%segmentation of the animal
% a: starting value for image
% b: last value for image
% N: every segment contains N points odd number
%
% every segment contains N points

    cd ('DATA_SAVED')
for s=a:b
point_segment_image=s
    PERXY=[];
    X=load(['skeleton_' int2str(s)]);
    Y=load(['edge_' int2str(s) '.txt']);
    

    
    
  
    X=[zeros(size(X,1),1),X,zeros(size(X,1),1)];
    X=[zeros(1,size(X,2));X;zeros(1,size(X,2))];
      start=[Y(1,1);Y(2,1)]+1;
   

   % figure;imshow(X)

    
    PERXY=start;
    aaa=1;
    while((length(find(X==1))>1))
        aaa=aaa+1;
        A=[];
        
       % start(1)
        %start(2)
        for i=start(1)-1:start(1)+1
            for j=start(2)-1:start(2)+1
                if (X(i,j)==1)  
                    
                    if (i==start(1) & j==start(2))
                        
                    else    
                    
                    A=[A;[j,i,sqrt((i-start(1))^2+(j-start(2))^2)]];
                    end
                end
               
            end
        end
 
      
      
      k=find(A(:,3)==min(A(:,3)));
      
        
        
        k=k(1);
        PERXY=[PERXY,[A(k,2);A(k,1)]];
        
        
    X(start(1),start(2))=0;
    
     L=bwlabel(X,8);
     L1=max(L(:));
     
     if L1>1
         
         size11=[];
         for w=1:L1
         ra=find(L==w);
         size11=[size11,length(ra)];
         end
         size11=size11;
         p=find(size11==max(size11));
         
         er=find(~(L==p));
         
         X(er)=0;
         A=[];
         for i=start(1)-2:start(1)+2
            for j=start(2)-2:start(2)+2
                if (X(i,j)==1)  
                    
                    if (i==start(1) & j==start(2))
                        
                    else    
                    
                    A=[A;[j,i,sqrt((i-start(1))^2+(j-start(2))^2)]];
                    end
                end
               
            end
        end
          k=find(A(:,3)==min(A(:,3)));
          start=[A(k,2);A(k,1)];
     
     else
    start=[A(k,2);A(k,1)];
       
    
    
     end
    end
    
    PERXY=PERXY-1;
    
 

%%%%%%%%%%%%%%%%%%%%%%%%%

n=N;
n1=rem(length(PERXY(1,:)),N);
n2=(length(PERXY(1,:))-n1)/N;
K=PERXY;

A=[];
A=n2*ones(N,1);
for i=1:n1
    A(i)=A(i)+1;
end

t=1;
while (t<=N)
    t=t;
    aq=['skeleton_' int2str(s) '_line=' int2str(t) '.txt'];
    K2=K(:,1:A(t));
    save(aq,'K2','-ascii');
    
    %Q=1+A(t)/2;
    Q1=mean(K2(1,:));
    Q2=mean(K2(2,:));Q=[Q1;Q2];
    Q=floor(Q);
    aq=['skeleton_' int2str(s) '_point=' int2str(t) '.txt'];
   % K2=K(:,Q);
   K2=Q;
    save(aq,'K2','-ascii');  
    K(:,1:A(t))=[];
    t=t+1;
    
   
    
end






end
cd ..