www.gusucode.com > matlab虚拟环境 找到迷宫的最短路径源码程序 > imagepro2.m
i=imread('arena03.bmp'); r=graythresh(i(:,:,1)); g=graythresh(i(:,:,2)); b=graythresh(i(:,:,3)); rm=r*255; gm=g*255; bm=b*255; %extracting nodes and starting and end point y=(i(:,:,1)>rm&i(:,:,2)>gm&i(:,:,3)==0); g1=(i(:,:,1)==0&i(:,:,2)==255&i(:,:,3)==0); r1=(i(:,:,1)==255&i(:,:,2)==0&i(:,:,3)==0); b1=(i(:,:,1)==0&i(:,:,2)==0&i(:,:,3)==255); ad=imadd(y,g1); bw4=im2bw(ad); ad2=imadd(bw4,r1); %finding the centroid of lines as well as nodes and catenating both %matrices li=(i(:,:,1)==0&i(:,:,2)==0&i(:,:,3)==0); [l,n]=bwlabel(ad2); s=regionprops(l,'Centroid'); cen=cat(1,s.Centroid); [m,n1]=bwlabel(li); s1=regionprops(m,'Centroid'); ce=cat(1,s1.Centroid); c=vertcat(cen,ce); n2=n+n1; %for k=1:n2 %text(c(k,1),c(k,2),int2str(k)); %end %making a connection matrix for v=1:n2 for u=1:n2 di=sqrt(((c(v,1)-c(u,1))^2)+((c(v,2)-c(u,2))^2)); if di>=23&&di<=27 a(v,u)=1; else a(v,u)=0; end end end %making a sparse matrix sp=sparse(a); [j,h]=bwlabel(r1); s2=regionprops(j,'Centroid'); ct=cat(1,s2.Centroid); %getting the label of red for o=1:n if(ct(1,1)==cen(o,1))&&(ct(1,2)==cen(o,2)) v1=o; end end [w,z]=bwlabel(g1); s4=regionprops(w,'Centroid'); ct1=cat(1,s4.Centroid); %geting the label of green for u=1:n if(ct1(1,1)==cen(u,1))&&(ct1(1,2)==cen(u,2)) v2=u; end end [dt,pa,pred]=graphshortestpath(sp,v1,v2,'Directed',false,'Method','BFS'); figure,image(i) %plotting the path for h=1:length(pa)-1 hold on,plot([c(pa(h),1),c(pa(h+1),1)],[c(pa(h),2),c(pa(h+1),2)],'r','linewidth',3); end