數字圖像處理-形態學操作

【轉載】:http://www.cnblogs.com/tornadomeet/archive/2012/03/20/2408086.html
Matlab 形態學圖像處理(原文作者很是細心,感謝!)

%% 第9章 形態學處理

%% imdilate膨脹
clc
clear

A1=imread('.\images\dipum_images_ch09\Fig0906(a)(broken-text).tif');
info=imfinfo('.\images\dipum_images_ch09\Fig0906(a)(broken-text).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次膨脹後的圖像');
 27%imdilate圖像膨脹處理過程運行結果如下:

%% imerode腐蝕
clc
clear
A1=imread('.\images\dipum_images_ch09\Fig0908(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)腐蝕後的圖像');
%圖像腐蝕處理過程運行結果如下:

%% 開運算和閉運算
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0910(a)(shapes).tif');
%se=strel('square',5');%方型結構元素
se=strel('disk',5');%圓盤型結構元素
imshow(f);%原圖像
title('開閉運算原始圖像')
 61%運行結果如下:

%開運算數學上是先腐蝕後膨脹的結果
%開運算的物理結果爲完全刪除了不能包含結構元素的對象區域,平滑
%了對象的輪廓,斷開了狹窄的連接,去掉了細小的突出部分
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('先閉後開運算');
 84%開閉運算結果如下:

%先膨脹再腐蝕
fse=imdilate(f,se);%膨脹

%gcf爲得到當前圖像的句柄,當前圖像是指例如PLOT,TITLE,SURF等
%get函數爲得到物體的屬性,get(0,'screensize')爲返回所有物體screensize屬性值
%set函數爲設置物體的屬性
figure,set(gcf,'outerposition',get(0,'screensize'));%具體目的是設置當前窗口的大小
subplot(211),imshow(fse);
title('使用disk(5)先膨脹後的圖像');

fes=imerode(fse,se);
subplot(212),imshow(fes);
title('使用disk(5)先膨脹再腐蝕後的圖像');
 99%先膨脹後腐蝕圖像如下:

%先腐蝕再膨脹
fse=imerode(f,se);
figure,set(gcf,'outerposition',get(0,'screensize'))
subplot(211),imshow(fse);
title('使用disk(5)先腐蝕後的圖像');

fes=imdilate(fse,se);
subplot(212),imshow(fes);
title('使用disk(5)先腐蝕再膨脹後的圖像');
110%先腐蝕後膨脹的圖像如下:

%% imopen imclose在指紋上的應用
clc
clear
f=imread('.\images\dipum_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('腐蝕後的指紋原始圖像');
123%指紋原始圖像和腐蝕後的圖像結果如下:

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)先閉後開操作後的圖像');
140%指紋圖像開閉操作過程結果如下:

%% bwhitmiss擊中或擊不中變換
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0913(a)(small-squares).tif');
imshow(f);
title('擊中或不擊中原始圖像');
148%擊中或不擊中原始圖像顯示結果如下:

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擊中擊不中的圖像');
172%擊中擊不中變換後圖像如下:

%%makelut
clc
clear

f=inline('sum(x(:))>=3');%inline是用來定義局部函數的
lut2=makelut(f,2)%爲函數f構造一個接收2*2矩陣的查找表
lut3=makelut(f,3)

%% Conway生命遊戲
clc
clear
lut=makelut(@conwaylaws,3);
bw1=  [0     0     0     0     0     0     0     0     0     0
       0     0     0     0     0     0     0     0     0     0
       0     0     0     1     0     0     1     0     0     0
       0     0     0     1     1     1     1     0     0     0
       0     0     1     0     0     0     0     1     0     0
       0     0     1     0     1     1     0     1     0     0
       0     0     1     0     0     0     0     1     0     0
       0     0     0     1     1     1     1     0     0     0
       0     0     0     0     0     0     0     0     0     0
       0     0     0     0     0     0     0     0     0     0  ];
subplot(221),imshow(bw1,'InitialMagnification','fit');
title('Generation 1');

bw2=applylut(bw1,lut);
subplot(222),imshow(bw2,'InitialMagnification','fit'),
title('Generation 2');

bw3=applylut(bw2,lut);
subplot(223),imshow(bw3,'InitialMagnification','fit');
title('Generation 3');

temp=bw1;
for i=2:100
    bw100=applylut(temp,lut);
    temp=bw100;
end
subplot(224),imshow(bw100,'InitialMagnification','fit')
title('Generation 100');
214%顯示Generation結果如下:

%% getsequence
clc
clear
se=strel('diamond',5)
decomp=getsequence(se)%getsequence函數爲得到分解的strel序列
decomp(1)
decomp(2)

%% endpoints
clc
clear

f1=imread('.\images\dipum_images_ch09\Fig0914(a)(bone-skel).tif');
subplot(121),imshow(f1);
title('原始形態骨架圖像');

g1=endpoints(f1);
%set(gcf,'outerposition',get(0,'screensize'));%運行完後自動生成最大的窗口
subplot(122),imshow(g1);
title('骨架圖像的端點圖像');
%骨架頭像端點檢測頭像如下:

f2=imread('.\images\dipum_images_ch09\Fig0916(a)(bone).tif');
figure,subplot(121),imshow(f2);
title('原始骨頭圖像');

g2=endpoints(f2);
subplot(122),imshow(g2);
title('骨頭圖像端點頭像');%結果是沒有端點
245%骨頭頭像端點檢測圖像如下:

%% bwmorph組合常見形態學之細化
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0911(a)(noisy-fingerprint).tif');
subplot(221),imshow(f);
title('指紋圖像細化原圖');

g1=bwmorph(f,'thin',1);
subplot(222),imshow(g1);
title('指紋圖像細化原圖');

g2=bwmorph(f,'thin',2);
subplot(223),imshow(g2);
title('指紋圖像細化原圖');

g3=bwmorph(f,'thin',Inf);
subplot(224),imshow(g3);
title('指紋圖像細化原圖');
265%指紋圖像細化過程顯示如下:

%% bwmorph組合常見形態學之骨骼化
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0911(a)(noisy-fingerprint).tif');
subplot(131),imshow(f);
title('指紋圖像骨骼化原圖');

fs=bwmorph(f,'skel',Inf);
subplot(132),imshow(fs);
title('指紋圖像骨骼化');

for k=1:5
    fs=fs&~endpoints(fs);
end
subplot(133),imshow(fs);
title('指紋圖像修剪後骨骼話');
283%指紋圖像骨骼化過程顯示:

%% 使用函數bwlabel標註連通分量
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0917(a)(ten-objects).tif');
imshow(f),title('標註連通分量原始圖像');
290%其結果顯示如下:

[L,n]=bwlabel(f);%L爲標記矩陣,n爲找到連接分量的總數
[r,c]=find(L==3);%返回第3個對象所有像素的行索引和列索引

rbar=mean(r);
cbar=mean(c);

figure,imshow(f)
hold on%保持當前圖像使其不被刷新
for k=1:n
    [r,c]=find(L==k);
    rbar=mean(r);
    cbar=mean(c);
    plot(cbar,rbar,'Marker','o','MarkerEdgeColor','k',...
         'MarkerFaceColor','k','MarkerSize',10);%這個plot函數用法不是很熟悉
    plot(cbar,rbar,'Marker','*','MarkerFaceColor','w');%其中的marker爲標記
end
title('標記所有對象質心後的圖像');

%% 由重構做開運算
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0922(a)(book-text).tif');
subplot(321),imshow(f);
title('重構原始圖像');

fe=imerode(f,ones(51,1));%豎線腐蝕
subplot(322),imshow(fe);
title('使用豎線腐蝕後的結果');

fo=imopen(f,ones(51,1));%豎線做開運算
subplot(323),imshow(fo);
title('使用豎線做開運算結果');

fobr=imreconstruct(fe,f);%fe做標記
subplot(324),imshow(fobr);
title('使用豎線做重構開運算');

ff=imfill(f,'holes');%對f進行孔洞填充
subplot(325),imshow(ff);
title('對f填充孔洞後的圖像');

fc=imclearborder(f,8);%清除邊界,28鄰接
subplot(326),imshow(fc);
title('對f清除邊界後的圖像');
336%圖像重構過程顯示如下:

%% 使用頂帽變換和底帽變換
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0926(a)(rice).tif');
subplot(221),imshow(f);
title('頂帽底帽變換原始圖像');

se=strel('disk',10);%產生結構元素
%頂帽變換是指原始圖像減去其開運算的圖像
%而開運算可用於補償不均勻的背景亮度,所以用一個大的結構元素做開運算後
%然後用原圖像減去這個開運算,就得到了背景均衡的圖像,這也叫做是圖像的頂帽運算
f1=imtophat(f,se);%使用頂帽變換
subplot(222),imshow(f1);
title('使用頂帽變換後的圖像');

%底帽變換是原始圖像減去其閉運算後的圖像
f2=imbothat(imcomplement(f),se);%使用底帽變換,爲什麼原圖像要求補呢?
%f2=imbothat(f,se);%使用底帽變換
subplot(223),imshow(f2);
title('使用底帽變換後的圖像');

%頂帽變換和底帽變換聯合起來用,用於增加對比度
f3=imsubtract(imadd(f,imtophat(f,se)),imbothat(f,se));%裏面參數好像不合理?
subplot(224),imshow(f3);
title('使用頂帽底帽聯合變換後圖像');
363%頂帽底帽變換過程圖像如下:

%%使用開運算和閉運算做形態學平滑
%由於開運算可以除去比結構元素更小的明亮細節,閉運算可以除去比結構元素更小的暗色細節
%所以它們經常組合起來一起進行平滑圖像並去除噪聲
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0925(a)(dowels).tif');
subplot(221),imshow(f);
title('木釘圖像原圖');

se=strel('disk',5);%disk其實就是一個八邊形
fo=imopen(f,se);%經過開運算
subplot(222),imshow(f);
title('使用半徑5的disk開運算後的圖像');

foc=imclose(fo,se);
subplot(223),imshow(foc);
title('先開後閉的圖像');

fasf=f;
for i=2:5
    se=strel('disk',i);
    fasf=imclose(imopen(fasf,se),se);
end
subplot(224),imshow(fasf);
title('使用開閉交替濾波後圖像');
390%使用開運算和閉運算做形態學平滑結果如下:

%% 顆粒分析
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0925(a)(dowels).tif');

sumpixels=zeros(1,36);
for k=0:35
    se=strel('disk',k);
    fo=imopen(f,se);
    sumpixels(k+1)=sum(fo(:));
end

%可以看到,連續開運算之間的表面積會減少
plot(0:35,sumpixels),xlabel('k'),ylabel('surface area');
title('表面積和結構元素半徑之間的關係');
407%其運算結果如下:   

figure,plot(-diff(sumpixels));%diff()函數爲差分或者近似倒數,即相鄰2個之間的差值
xlabel('k'),ylabel('surface area reduction');
title('減少的表面積和結構元素半徑之間的關係');
412%其運算結果如下:

%% 使用重構刪除複雜圖像的背景
clc
clear
f=imread('.\images\dipum_images_ch09\Fig0930(a)(calculator).tif');
subplot(221),imshow(f);
title('灰度級重構原圖像');

f_obr=imreconstruct(imerode(f,ones(1,71)),f);
subplot(222),imshow(f_obr);
title('經開運算重構圖');

f_o=imopen(f,ones(1,71));
subplot(223),imshow(f_o);
title('經開運算後圖');

f_thr=imsubtract(f,f_obr);
subplot(224),imshow(f_thr);
title('頂帽運算重構圖')
432%使用重構刪除複雜圖像的背景1:

f_th=imsubtract(f,f_o)
figure,subplot(221),imshow(f_th);
title('經頂帽運算圖');

g_obr=imreconstruct(imerode(f_thr,ones(1,11)),f_thr);
subplot(222),imshow(g_obr);
title('用水平線對f_thr經開運算後重構圖');

g_obrd=imdilate(g_obr,ones(1,2));
subplot(223),imshow(g_obrd);
title('使用水平線對上圖進行膨脹');

f2=imreconstruct(min(g_obrd,f_thr),f_thr);
subplot(224),imshow(f2);
title('最後的重構結果');
449%使用重構刪除複雜圖像的背景2:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章