1. 程式人生 > >【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次膨脹後的影象');


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)腐蝕後的影象');



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('先閉後開運算');


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)先閉後開操作後的影象');



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擊中擊不中的影象');