www.pudn.com > ridge_extract.rar > back_follow.m, change:2007-05-08,size:4363b


function [ri,le_left,e_left,in]=back_follow(startx,starty,ridge,I,D,i) 
%-------------------------- 
%向后跟踪 
%------------------------- 
fid = fopen('f_back.txt','w'); 
in=i;%即将要跟踪的脊线标号 
overlap=0;%是否跟踪到重复的脊线标志位,‘0’表示没有重复,‘1’表示重复 
end_left=zeros(1,2);%左终点坐标 
% ridge=zeros(size(I));%脊线轨迹矩阵 
length_left=0;%左边脊线长度 
signal=0;%是否发生回退信号,‘1’表示没有发生 
ridge(startx,starty)=i; 
[r,c]=size(I); 
pre_x=startx; 
pre_y=starty; 
p_x=startx; 
p_y=starty; 
next_x=0; 
next_y=0; 
section=zeros(3,2); 
       %正向跟踪 
 while 1 
    signal=0; 
    if p_y-1>=1 
        section(2,1)=I(p_x,p_y-1); 
    else 
        section(2,1)=1; 
    end 
    if (p_x-1>=1) 
        section(1,2)=I(p_x-1,p_y); 
    else 
        section(1,2)=1; 
    end 
    if (p_x-1>=1)&(p_y-1>=1) 
        section(1,1)=I(p_x-1,p_y-1); 
    else 
        section(1,1)=1; 
    end 
    if (p_x+1=r) 
        section(3,2)=I(p_x+1,p_y); 
    else 
        section(3,2)=1; 
    end 
    if ((p_x+1)=r)&(p_y-1>=1) 
        section(3,1)=I(p_x+1,p_y-1); 
    else 
        section(3,1)=1; 
    end 
    section(2,2)=~I(p_x,p_y); 
 while signal==0 
    [xn,yn,sn]=find(~section); 
    [rn,cn]=size(xn); 
    switch rn 
        case 0 
            [next_x,next_y]=five_ray(p_x,p_y,I,D,1); 
            if (next_x==0)&(next_y==0) 
                end_left(1)=p_x; 
                end_left(2)=p_y; 
                break; 
            end 
        case 1 
            next_x=p_x+xn-2; 
            next_y=p_y+yn-2; 
        otherwise 
            if (D(p_x,p_y)==1)&((p_y-1)>=1)&(section(2,1)==0) 
                next_x=p_x; 
                next_y=p_y-1; 
            elseif (D(p_x,p_y)==2)&((p_y-1)>=1)&((p_x+1)=r)&(section(3,1)==0) 
                next_x=p_x+1; 
                next_y=p_y-1; 
            elseif (D(p_x,p_y)==3) 
                if (section(3,2)==0)&(p_x+1=r) 
                    next_x=p_x+1; 
                    next_y=p_y; 
                elseif (section(1,2)==0)&((p_x-1)>=1) 
                    next_x=p_x-1; 
                    next_y=p_y; 
                end 
            elseif (D(p_x,p_y)==4)&((p_y-1)>=1)&((p_x-1)>=1)&(section(1,1)==0) 
                next_x=p_x-1; 
                next_y=p_y-1; 
            elseif section(3,1)==0%克服点方向错误,只要有邻接点就往下走 
                next_x=p_x+1; 
                next_y=p_y-1; 
            elseif section(2,1)==0 
                next_x=p_x; 
                next_y=p_y-1; 
            elseif section(1,1)==0 
                next_x=p_x-1; 
                next_y=p_y-1; 
            elseif section(3,2)==0 
                next_x=p_x+1; 
                next_y=p_y; 
            elseif section(1,2)==0 
                next_x=p_x-1; 
                next_y=p_y; 
            else 
             [next_x,next_y]=five_ray(p_x,p_y,I,D,1); 
             if (next_x==0)&(next_y==0) 
                 end_left(1)=p_x; 
                 end_left(2)=p_y; 
                 break; 
             end 
            end 
     end %The end of 'switch' 
    if (next_x==pre_x)&(next_y==pre_y) 
        signal=0; 
        section(2+next_x-p_x,2+next_y-p_y)=1; 
    else 
        signal=1; 
    end 
end 
  if (next_x==0)&(next_y==0) 
    break; 
   end 
  
      if (ridge(next_x,next_y)>0)&(ridge(next_x,next_y)<i)%下一个点在已经跟踪过的脊线上 
            for ik=1:r 
                for jk=1:c 
                    if  ridge(ik,jk)==i 
                        ridge(ik,jk)=ridge(next_x,next_y); 
                    end 
                end 
            end 
             i=ridge(next_x,next_y); 
            if overlap==0  %若在这之前没有发生过重复的情况,即将要跟踪的脊线标号减1 
                in=in-1; 
            end 
            %当前跟踪的脊线对该部分的脊线长不影响 
                length_left=0; 
                overlap=1; 
            elseif (ridge(next_x,next_y)>0)&(ridge(next_x,next_y)==i) 
                end_right(1)=p_x; 
                end_right(2)=p_y; 
                break; 
        else 
            ridge(next_x,next_y)=i; 
            fprintf(fid,'%d %d   ', next_x,next_y); 
            if overlap==0 
              length_left=length_left+1; 
            end 
        end 
         pre_x=p_x; 
         pre_y=p_y; 
         p_x=next_x; 
         p_y=next_y; 
end 
fclose(fid); 
ri=ridge; 
le_left=length_left; 
e_left=end_left;