clear;I=imread('E:\matlab\'); %输入图像O=rgb2ntsc(I);G=O(:,:,2);[m n]=size(G);U=zeros(m,n);for i=1:mfor j=1:nif G(i,j)>(i,j)<(i,j)=1;endendendsr=strel('disk',6);C=imclose(U,sr);L=bwlabel(C);B=regionprops(L,'area');Se=[];Sm=max(Se);if Sm>m*n/27B1=bwareaopen(C,Sm);k_y1=m;k2=m;l2=n;for i=1:mif any(B1(i,:))==1k_y1=i;breakendendfor i=k_y1:mif B1(i,:)==0k2=i;breakendendfor j=1:nif any(B1(:,j))==1l_y1=j;breakendendfor j=l_y1:nif B1(:,j)==0l2=j;breakendendk_y=k2-k_y1;l=l2-l_y1;if k_y>.5*l&&k_y<3*lI1=imcrop(B1,[l_y1 k_y1 l .4*k_y]);[n1 m1]=size(I1);L1=bwlabel(I1);E=regionprops(L1,'area');Si=[];Sm=max(Si);if Sm/(n1*m1)>.3B2=bwareaopen(I1,floor(.5*Sm));g_y1=m1;g2=m1;for j=1:m1if any(B2(:,j))==1g_y1=j;breakendendfor j=g_y1:m1if B2(:,j)==0;g2=j;breakendendg=g2-g_y1;figure;imshow(I);hold onh1=line([l_y1+g_y1,l_y1+g_y1+g],[k_y1,k_y1]);h2=line([l_y1+g_y1+g,l_y1+g_y1+g],[k_y1,k_y1+*g]);h3=line([l_y1+g_y1+g,l_y1+g_y1],[k_y1+*g,k_y1+*g]);h4=line([l_y1+g_y1,l_y1+g_y1],[k_y1+*g,k_y1]);h=[h1 h2 h3 h4];set(h,'Color',[1 0 0],'LineWidth',2);elsefigure;imshow(I);endelsefigure;imshow(I);endelsefigure;imshow(I);end给分!