matlab腐蚀膨胀开运算,【Matlab学习笔记】【数学形态学】膨胀、腐蚀、开运算、闭运算、击中击不中变换...

1.形态学膨胀操作:

%膨胀 imdilate(dilate=膨胀/扩大)

clc

clear

A1=imread('C:\Users\Administrator\Pictures\DIP3E_CH09_Original_Images\DIP3E_Original_Images_CH09\Fig0907(a)(text_gaps_1_and_2_pixels).tif');

info=imfinfo('C:\Users\Administrator\Pictures\DIP3E_CH09_Original_Images\DIP3E_Original_Images_CH09\Fig0907(a)(text_gaps_1_and_2_pixels).tif');

B=[0 1 0

1 1 1

0 1 0];

A2=imdilate(A1,B);%图像A1被结构元素B膨胀

A3=imdilate(A2,B);

A4=imdilate(A3,B);

subplot(221),imshow(A1);

title('imdilate膨胀原始图像');

subplot(222),imshow(A2);

title('使用B后1次膨胀后的图像');

subplot(223),imshow(A3);

title('使用B后2次膨胀后的图像');

subplot(224),imshow(A4);

title('使用B后3次膨胀后的图像');

0818b9ca8b590ca3270a3433284dd417.png

2.形态学腐蚀操作:

%腐蚀 imerode(erode=腐蚀、侵蚀)

clc

clear

A1=imread('C:\Users\Administrator\Desktop\形态学操作\DIP3E_CH09_Original_Images\DIP3E_Original_Images_CH09\Fig0905(a)(wirebond-mask).tif');subplot(221),imshow(A1);

title('腐蚀原始图像');

%strel函数的功能是运用各种形状和大小构造结构元素

se1=strel('disk',5);%这里是创建一个半径为5的平坦型圆盘结构元素

A2=imerode(A1,se1);

subplot(222),imshow(A2);

title('使用结构原始disk(5)腐蚀后的图像');

se2=strel('disk',10);

A3=imerode(A1,se2);

subplot(223),imshow(A3);

title('使用结构原始disk(10)腐蚀后的图像');

se3=strel('disk',20);

A4=imerode(A1,se3);

subplot(224),imshow(A4);

title('使用结构原始disk(20)腐蚀后的图像');

0818b9ca8b590ca3270a3433284dd417.png

3.形态学开运算操作:

%开运算和闭运算

clc

clear

f=imread('C:\Users\Administrator\Desktop\形态学操作\DIP3E_CH09_Original_Images\DIP3E_Original_Images_CH09\FigP0917(noisy_rectangle).tif');

%se=strel('square',10');%方型结构元素

se=strel('disk',20');%圆盘型结构元素

imshow(f);%原图像

title('开闭运算原始图像');

%开运算数学上是先腐蚀后膨胀的结果 %开运算的物理结果为完全删除了不能包含结构元素的对象区域,平滑

%了对象的轮廓,断开了狭窄的连接,去掉了细小的突出部分

fo=imopen(f,se);%直接开运算

figure,subplot(221),imshow(fo);

title('直接开运算');

%闭运算在数学上是先膨胀再腐蚀的结果

%闭运算的物理结果也是会平滑对象的轮廓,但是与开运算不同的是,闭运算

%一般会将狭窄的缺口连接起来形成细长的弯口,并填充比结构元素小的洞

fc=imclose(f,se);%直接闭运算

subplot(222),imshow(fc);

title('直接闭运算');

foc=imclose(fo,se);%先开后闭运算

subplot(223),imshow(foc);

title('先开后闭运算');

fco=imopen(fc,se);%先闭后开运算

subplot(224),imshow(fco);

title('先闭后开运算');

0818b9ca8b590ca3270a3433284dd417.png

4.形态学闭运算操作:

%腐蚀与开闭运算在指纹图像上的应用对比

clc

clear

f=imread('C:\Users\Administrator\Desktop\形态学操作\DIP3E_CH09_Original_Images\DIP3E_Original_Images_CH09\Fig0911(a)(noisy_fingerprint).tif');

se=strel('square',3);%边长为3的方形结构元素

subplot(121),imshow(f);

title('指纹原始图像');

A=imerode(f,se);%腐蚀

subplot(122),imshow(A);

title('腐蚀后的指纹原始图像');

fo=imopen(f,se); figure,subplot(221),imshow(fo);

title('使用square(3)开操作后的图像');

fc=imclose(f,se);

subplot(222),imshow(fc);

title('使用square闭操作后的图像');

foc=imclose(fo,se);

subplot(223),imshow(foc);

title('使用square(3)先开后闭操作后的图像')

fco=imopen(fc,se);

subplot(224),imshow(fco);

title('使用square(3)先闭后开操作后的图像');

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

5.形态学击中击不中操作:

%击中击不中变换

%其基本原理为:(集合X为原二值化图像的像素集合,对X取反求得~X(非X, Y表示), 选

%择的结构元为s1, 对结构元s1取反的结构元为s2)

%首先对用s1对X进行腐蚀得到A1,, 用s2对Y(即~X)进行腐蚀得到A2。

%最终结果C = A1 & A2。

clc

clear

f=imread('C:\Users\Administrator\Desktop\形态学操作\DIP3E_CH09_Original_Images\DIP3E_Original_Images_CH09\FigP0918(left).tif');

imshow(f);

title('击中或不击中原始图像');

B1=strel([0 0 0;0 1 1;0 1 0]);%击中:要求击中所有1的位置

B2=strel([1 1 1;1 0 0;1 0 0]);%击不中,要求击不中所有1的位置

B3=strel([0 1 0;1 1 1;0 1 0]);%击中

B4=strel([1 0 1;0 0 0;0 0 0]);%击不中

B5=strel([0 0 0;0 1 0;0 0 0]);%击中

B6=strel([1 1 1;1 0 0;1 0 0]);%击不中

g=imerode(f,B1)&imerode(~f,B2)%利用定义来实现击中或击不中

figure,subplot(221),imshow(g);

title('定义实现组1击中击不中图像');

g1=bwhitmiss(f,B1,B2);

subplot(222),imshow(g1);

title('结构数组1击中击不中后的图像');

g2=bwhitmiss(f,B3,B4);

subplot(223),imshow(g2);

title('结构数组2击中击不中的图像');

g3=bwhitmiss(f,B5,B6);

subplot(224),imshow(g3);

title('结构数组3击中击不中的图像');

0818b9ca8b590ca3270a3433284dd417.png

你可能感兴趣的:(matlab腐蚀膨胀开运算)