clear,clc;h=imread('ceshi2.bmp');i=im2bw(h);i1i=187;i1j=192;forai=181:193foraj=186:198if(sqrt(double(ai-i1i)^2+double(aj-i1j)^2)=5)i(ai,aj)=1;%定义圆形结构元素endendendfigure,imshow(i);i1=i;fori1i=6:205%用B腐蚀Afori1j=6:205flag=0;if(i1i=181&&i1i=193&&i1j=186&&i1j=198)continue;elseif(i(i1i,i1j)==1)forai=i1i-5:i1i+5foraj=i1j-5:i1j+5if(i1(ai,aj)==0&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))=5)i(i1i,i1j)=0;flag=1;break;endendif(flag==1)break;endendendendendendfigure,imshow(i);%在上面C的图像上用B进行膨胀i2=i;fori1i=6:205%用B膨胀Cfori1j=6:205flag=0;if(i1i=175&&i1i=199&&i1j=180&&i1j=204)continue;elseforai=i1i-5:i1i+5foraj=i1j-5:i1j+5if(i2(ai,aj)==1&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))=5)i(i1i,i1j)=1;flag=1;break;endendif(flag==1)break;endendendendendfigure,imshow(i);%在上面D的图像上用B进行膨胀i2=i;fori1i=6:205%用B膨胀Dfori1j=6:205flag=0;if(i1i=175&&i1i=199&&i1j=180&&i1j=204)continue;elseforai=i1i-5:i1i+5foraj=i1j-5:i1j+5if(i2(ai,aj)==1&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))=5)i(i1i,i1j)=1;flag=1;break;endendif(flag==1)break;endendendendendfigure,imshow(i);%在上面E的图像上用B进行腐蚀i1=i;fori1i=6:205%用B腐蚀Efori1j=6:205flag=0;if(i1i=181&&i1i=193&&i1j=186&&i1j=198)continue;elseif(i(i1i,i1j)==1)forai=i1i-5:i1i+5foraj=i1j-5:i1j+5if(i1(ai,aj)==0&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))=5)i(i1i,i1j)=0;flag=1;break;endendif(flag==1)break;endendendendendendfigure,imshow(i);