function numb= correct_skeleton(a,b,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
%
%
% a: starting image
% b: ending image
% A: coordinates of head  76 139    
% B: coordinates of tail  170 195    
%A-->A+1
%B-->B+1


for s=a:b
    
    cd ('DATA_SAVED')
    
    s=s;
    sk=load(['skeleton_' int2str(s)]);
    
    cd ..

   cd ('DATA_SAVED')
   file2=['skeleton_' int2str(s)];
   save(file2,'sk','-ascii');
   numb=[s;length(find(sk==1))];
   cd ..
   cd ('DATA_SAVED')
    
    
       sk=[zeros(size(sk,1),1),sk,zeros(size(sk,1),1)];
    sk=[zeros(1,size(sk,2));sk;zeros(1,size(sk,2))];
    
    W=bwmorph(sk,'spur');
    
    endpoints=sk-W;
    
    %figure;imshow(sk);hold on;plot([139,195]+1,[76 170]+1,'*');
    
    

    M=[];
    [r,c]=find(endpoints==1);
    
    size1=length(r(:));
    for t=1:length(r(:))
      M=[M;[t,r(t), c(t)]];
        
        
    end
    
   %figure;imshow(sk);
   %hold on; plot(M(:,3),M(:,2),'r.');
    %if s==110
        
     %   dfdsf
     %end
   START=[];END=[];
   
   for t=1:size1
   
       START=[START;[M(t,1) M(t,2) M(t,3) (M(t,2)-A(1))^2+(M(t,3)-A(2))^2]];
       END=[END;[M(t,1) M(t,2) M(t,3) (M(t,2)-B(1))^2+(M(t,3)-B(2))^2]];
   end
   START;
   END;
   
   k=find(START(:,end)==min(START(:,end)));
   START_value=[M(k(1),2); M(k(1),3) ];
   k1=find(END(:,end)==min(END(:,end)));
   END_value=[M(k1(1),2); M(k1(1),3) ];
   
     K2=[START_value-1, END_value-1];
  
     
    cd ..
   cd ('DATA_SAVED')
    aq=['edge_' int2str(s) '.txt'];
    save(aq,'K2','-ascii'); 
   
   M([k(1) k1(1)],:)=[];     %%%%% the points which are the ends of tails that should be removed
   
   
   
   A_new=START_value;
   B_new=END_value;
   A=A_new;
   B=B_new;
  
  

 cd ..
end