形态学——边界提取

  集合 A 的边界表示为 β(A),它可以通过先由 B 对 A 腐蚀,而后用 A 减去腐蚀得到。即:

                

 

 

 

 代码

clear;clc;
width=500;  %图像的宽
height=500;  %图像的高
expand_size=200;

img=ones(width,height);
expand_img=double(wextend('2D','zpd',img,expand_size));%扩展0,也就是增加padding
model=ones(51,51);%模板大小为15
model_size=51;
cen=floor((model_size+1)/2);
Sum=model_size*model_size;

[M,N]=size(expand_img);
img_result=zeros(M,N);
for i=1:(M-model_size)
    for j=1:(N-model_size)
        ave=sum(sum(expand_img(i:i+model_size-1,j:j+model_size-1).*model));
        if ave==Sum
            img_result(i+cen,j+cen)=255;
        end
    end
end
figure
subplot(1,2,1);
imshow(expand_img)
title('原图像');
subplot(1,2,2);
imshow(expand_img-img_result)
title('边缘提取之后的图像');

 

 

clear;clc;
model=double(ones(3,3));%模板大小为51
model_size=3;
top=model_size*model_size*255*0.6;
cen=floor((model_size+1)/2);
Sum=model_size*model_size;
img=imread('lena.jpg');
expand_img=im2bw(img);

[M,N]=size(expand_img);
img_result=zeros(M,N);
for i=1:(M-model_size)
    for j=1:(N-model_size)
        ave=sum(sum(expand_img(i:i+model_size-1,j:j+model_size-1).*model));
        if ave==Sum
            img_result(i+cen,j+cen)=255;
        end
    end
end
figure
subplot(1,2,1);
imshow(expand_img)
title('原图像');
subplot(1,2,2);
imshow(expand_img-img_result)
title('边缘提取之后的图像');

 

 

f=imread('lena.jpg');
figure;
subplot(2,2,1);
imshow(f);
title('原图');
f=rgb2gray(f);
f=im2bw(f);%图像二值化
subplot(2,2,2);
imshow(f);
title('人脸二值图像');
se=strel('disk',3);%选取3*3正方形结构元素
Ie=imerode(f,se);%对原图像进行腐蚀,
Iout1=f-Ie;%原图像减去腐蚀结果
subplot(2,2,3);
imshow(Iout1);
title('直接边界轮廓提取')
Iout2=bwperim(f,4);%用bwperim提取边界
subplot(2,2,4);
imshow(Iout2);
title('用bwperim提取边界')

posted @ 2021-01-14 13:18  为红颜  阅读(26)  评论(0编辑  收藏