clear,clc
h=imread('ceshi2.bmp')
i=im2bw(h)
i1i=187
i1j=192
for ai=181:193
for aj=186:198
if(sqrt(double(ai-i1i)^2+double(aj-i1j)^2)<=5)
i(ai,aj)=1%定义圆形结构元素
end
end
end
figure,imshow(i)
i1=i
for i1i=6:205%用B腐蚀A
for i1j=6:205
flag=0
if(i1i>=181&&i1i<=193&&i1j>=186&&i1j<=198)
continue
else
if(i(i1i,i1j)==1)
for ai=i1i-5:i1i+5
for aj=i1j-5:i1j+5
if(i1(ai,aj)==0&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)
i(i1i,i1j)=0
flag=1
break
end
end
if(flag==1)
break
end
end
end
end
end
end
figure,imshow(i)
i2=i
for i1i=6:205%用B膨胀C
for i1j=6:205
flag=0
if(i1i>=175&&i1i<=199&&i1j>=180&&i1j<=204)
continue
else
for ai=i1i-5:i1i+5
for aj=i1j-5:i1j+5
辩胡 if(i2(ai,aj)==1&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)
i(i1i,i1j)=1
flag=1
break
end
end
if(flag==1)
break
end
end
end
end
end
figure,imshow(i)
%在上面D的图像上用B进行膨胀
i2=i
for i1i=6:205%用B膨胀D
for i1j=6:205
flag=0
if(i1i>=175&&i1i<=199&&i1j>=180&&i1j<=204)
continue
else
for ai=i1i-5:i1i+5
for aj=i1j-5:i1j+5
if(i2(ai,aj)==1&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)
i(i1i,i1j)=1
flag=1
break
end
end
if(flag==1)
break
扮灶春end
end
end
end
end
figure,imshow(i)
%在上面E的图像上用B进行腐蚀
i1=i
for i1i=6:205%用B腐蚀E
for i1j=6:205
flag=0
if(i1i>=181&&i1i<=193&&i1j>=186&&i1j<=198)
continue
else
if(i(i1i,i1j)==1)
for ai=i1i-5:i1i+5
for aj=i1j-5:i1j+5
if(i1(ai,aj)==0&&sqrt(double((ai-i1i)^2+(aj-i1j)^2))<=5)
i(i1i,i1j)=0
flag=1
break
厅耐 end
end
if(flag==1)
break
end
end
end
end
end
end
figure,imshow(i)
matlab有对二值图像进行膨胀腐蚀等升烂形态学处理的函数imdilate
imerode
还哗昌有bwlabel函数,可以将图像中不同的连通分量(不同的对象)进行标记
可以了用bwlabel取得只有不同对象的二值图像
单独吵芦漏对其进行形态学处理
要画直线连接两个对象,要看你具体有什么要求
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)