数字图像处理腐蚀与膨胀小程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
%%%%%%%%%%%%%%%%下面为用于处理的样本‘圆’%%%%%%%%%%%%%%%%%%%%%%%%%
R=8;%%半径R
S=zeros(2*R);
for x1=-R:R-1;
for y1=-R:R-1;
if round(sqrt(x1^2+y1^2))<=R%%圆心在原点的圆的表达函数
%%对圆心在原点的圆进行平移,使x,y都为非负数
x=R+x1+1;
y=R+y1+1;
S(x,y)=1;
end
end
end
figure(1),imshow(S); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%下面为要处理的图形%%%%%%%%%%%%%%%%%%%%%%%%%
W=256;L=512; %%定义边界
X=zeros(W,L);
for i=1:128
for j=1:256
if i<=15|i>=114
if j<103|j>153
X(i+W/4,j+L/4)=1;
end
else if (i>15&i<=56)|(i>=72&i<114)
if (j>50&j<103)|j>153
X(i+W/4,j+L/4)=1;
end
else if i>56&i<72
if j>50&j<206
X(i+W/4,j+L/4)=1;
end
end
end
end
end
end
figure(2),imshow(X); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%下面用S对X进行腐蚀操
作%%%%%%%%%%%%%%%%%%%%%%%%
X1=zeros(W,L);%X1用于存放处理后的图像
M=zeros(2*R);
for m=0:W-2*R
for n=0:L-2*R
%%下面用S对X区域进行‘与’操作%%
for i=1:2*R
for j=1:2*R
M(i,j)=X(i+m,j+n)&S(i,j);
end
end
if M==S %%判断S是否处于X中,等则在S中
X1(m+R,n+R)=1;
end
M=zeros(2*R);
end
end
figure(3),imshow(X1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%下面用S对X1进行膨胀操作%%%%%%%%%%%%%%%%%%%%%%%%%%
X2=zeros(W,L);%X2用于存放处理后的图像
cnt=0;
for m=0:W-2*R
for n=0:L-2*R
%%下面用S对X区域进行‘与’操作%%
for i=1:2*R
for j=1:2*R
if X1(i+m,j+n)&S(i,j)==1 %%判断S是否与X相交
cnt=1;
break;
end
end
if cnt==1 %%判断是否是由于cnt=1而跳出循环
break;
end
end
if cnt==1 %%判断是否是由于cnt=1而跳出循环
X2(m+R,n+R)=1;
cnt=0; %%将cnt的值置0
end
end
end
figure(4),imshow(X2); ( S )
( X )
( X1 )
( X2 )