形态学图像处理--matlab

转自:http://blog.csdn.net/hesays/article/details/41850873

Matlab 形态学图像处理

    本章的练习主要是形态学的一些基本概念和技术,这些构成了一组提取图像特征的有力工具,针对二值图像和灰度图像的腐蚀、膨胀和重构的基本操作可以组合使用,以执行非常宽泛的任务。其练习代码和结果如下

[plain]  view plain  copy
  1. %% 第9章 形态学处理  
  2.   
  3.   
  4. %% imdilate膨胀  
  5. clc  
  6. clear  
  7.   
  8.   
  9. A1=imread('.\images\dipum_images_ch09\Fig0906(a)(broken-text).tif');  
  10. info=imfinfo('.\images\dipum_images_ch09\Fig0906(a)(broken-text).tif')  
  11. B=[0 1 0  
  12.    1 1 1  
  13.    0 1 0];  
  14. A2=imdilate(A1,B);%图像A1被结构元素B膨胀  
  15. A3=imdilate(A2,B);  
  16. A4=imdilate(A3,B);  
  17.   
  18.   
  19. subplot(221),imshow(A1);  
  20. title('imdilate膨胀原始图像');  
  21.   
  22.   
  23. subplot(222),imshow(A2);  
  24. title('使用B后1次膨胀后的图像');  
  25.   
  26.   
  27. subplot(223),imshow(A3);  
  28. title('使用B后2次膨胀后的图像');  
  29.   
  30.   
  31. subplot(224),imshow(A4);  
  32. title('使用B后3次膨胀后的图像');  


图像膨胀处理过程运行结果如下:



[plain]  view plain  copy
  1. %% imerode腐蚀  
  2. clc  
  3. clear  
  4. A1=imread('.\images\dipum_images_ch09\Fig0908(a)(wirebond-mask).tif');  
  5. subplot(221),imshow(A1);  
  6. title('腐蚀原始图像');  
  7.   
  8.   
  9. %strel函数的功能是运用各种形状和大小构造结构元素  
  10. se1=strel('disk',5);%这里是创建一个半径为5的平坦型圆盘结构元素  
  11. A2=imerode(A1,se1);  
  12. subplot(222),imshow(A2);  
  13. title('使用结构原始disk(5)腐蚀后的图像');  
  14.   
  15.   
  16. se2=strel('disk',10);  
  17. A3=imerode(A1,se2);  
  18. subplot(223),imshow(A3);  
  19. title('使用结构原始disk(10)腐蚀后的图像');  
  20.   
  21.   
  22. se3=strel('disk',20);  
  23. A4=imerode(A1,se3);  
  24. subplot(224),imshow(A4);  
  25. title('使用结构原始disk(20)腐蚀后的图像');  

图像腐蚀处理过程运行结果如下:



[html]  view plain  copy
  1. %% 开运算和闭运算  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0910(a)(shapes).tif');  
  5. %se=strel('square',5');%方型结构元素  
  6. se=strel('disk',5');%圆盘型结构元素  
  7. imshow(f);%原图像  
  8. title('开闭运算原始图像')  


运行结果如下:



[html]  view plain  copy
  1. %开运算数学上是先腐蚀后膨胀的结果  
  2. %开运算的物理结果为完全删除了不能包含结构元素的对象区域,平滑  
  3. %了对象的轮廓,断开了狭窄的连接,去掉了细小的突出部分  
  4. fo=imopen(f,se);%直接开运算  
  5. figure,subplot(221),imshow(fo);  
  6. title('直接开运算');  
  7.   
  8.   
  9. %闭运算在数学上是先膨胀再腐蚀的结果  
  10. %闭运算的物理结果也是会平滑对象的轮廓,但是与开运算不同的是,闭运算  
  11. %一般会将狭窄的缺口连接起来形成细长的弯口,并填充比结构元素小的洞  
  12. fc=imclose(f,se);%直接闭运算  
  13. subplot(222),imshow(fc);  
  14. title('直接闭运算');  
  15.   
  16.   
  17. foc=imclose(fo,se);%先开后闭运算  
  18. subplot(223),imshow(foc);  
  19. title('先开后闭运算');  
  20.   
  21.   
  22. fco=imopen(fc,se);%先闭后开运算  
  23. subplot(224),imshow(fco);  
  24. title('先闭后开运算');  



开闭运算结果如下:



[html]  view plain  copy
  1. %先膨胀再腐蚀  
  2. fse=imdilate(f,se);%膨胀  
  3.   
  4.   
  5. %gcf为得到当前图像的句柄,当前图像是指例如PLOT,TITLE,SURF等  
  6. %get函数为得到物体的属性,get(0,'screensize')为返回所有物体screensize属性值  
  7. %set函数为设置物体的属性  
  8. figure,set(gcf,'outerposition',get(0,'screensize'));%具体目的是设置当前窗口的大小  
  9. subplot(211),imshow(fse);  
  10. title('使用disk(5)先膨胀后的图像');  
  11.   
  12.   
  13. fes=imerode(fse,se);  
  14. subplot(212),imshow(fes);  
  15. title('使用disk(5)先膨胀再腐蚀后的图像');  


先膨胀后腐蚀图像如下:




[html]  view plain  copy
  1. %先腐蚀再膨胀  
  2. fse=imerode(f,se);  
  3. figure,set(gcf,'outerposition',get(0,'screensize'))  
  4. subplot(211),imshow(fse);  
  5. title('使用disk(5)先腐蚀后的图像');  
  6.   
  7.   
  8. fes=imdilate(fse,se);  
  9. subplot(212),imshow(fes);  
  10. title('使用disk(5)先腐蚀再膨胀后的图像');  



先腐蚀后膨胀的图像如下:


[html]  view plain  copy
  1. %% imopen imclose在指纹上的应用  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0911(a)(noisy-fingerprint).tif');  
  5. se=strel('square',3);%边长为3的方形结构元素  
  6. subplot(121),imshow(f);  
  7. title('指纹原始图像');  
  8.   
  9.   
  10. A=imerode(f,se);%腐蚀  
  11. subplot(122),imshow(A);  
  12. title('腐蚀后的指纹原始图像');  



指纹原始图像和腐蚀后的图像结果如下:



[html]  view plain  copy
  1. fo=imopen(f,se);  
  2. figure,subplot(221),imshow(fo);  
  3. title('使用square(3)开操作后的图像');  
  4.   
  5.   
  6. fc=imclose(f,se);  
  7. subplot(222),imshow(fc);  
  8. title('使用square闭操作后的图像');  
  9.   
  10.   
  11. foc=imclose(fo,se);  
  12. subplot(223),imshow(foc);  
  13. title('使用square(3)先开后闭操作后的图像')  
  14.   
  15.   
  16. fco=imopen(fc,se);  
  17. subplot(224),imshow(fco);  
  18. title('使用square(3)先闭后开操作后的图像');  


指纹图像开闭操作过程结果如下:



[html]  view plain  copy
  1. %% bwhitmiss击中或击不中变换  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0913(a)(small-squares).tif');  
  5. imshow(f);  
  6. title('击中或不击中原始图像');  


击中或不击中原始图像显示结果如下:



[html]  view plain  copy
  1. B1=strel([0 0 0;0 1 1;0 1 0]);%击中:要求击中所有1的位置  
  2. B2=strel([1 1 1;1 0 0;1 0 0]);%击不中,要求击不中所有1的位置  
  3. B3=strel([0 1 0;1 1 1;0 1 0]);%击中  
  4. B4=strel([1 0 1;0 0 0;0 0 0]);%击不中  
  5. B5=strel([0 0 0;0 1 0;0 0 0]);%击中  
  6. B6=strel([1 1 1;1 0 0;1 0 0]);%击不中  
  7.   
  8.   
  9. g=imerode(f,B1)&imerode(~f,B2)%利用定义来实现击中或击不中  
  10. figure,subplot(221),imshow(g);  
  11. title('定义实现组1击中击不中图像');  
  12.   
  13.   
  14. g1=bwhitmiss(f,B1,B2);  
  15. subplot(222),imshow(g1);  
  16. title('结构数组1击中击不中后的图像');  
  17.   
  18.   
  19. g2=bwhitmiss(f,B3,B4);  
  20. subplot(223),imshow(g2);  
  21. title('结构数组2击中击不中的图像');  
  22.   
  23.   
  24. g3=bwhitmiss(f,B5,B6);  
  25. subplot(224),imshow(g3);  
  26. title('结构数组3击中击不中的图像');  



击中击不中变换后图像如下:



[html]  view plain  copy
  1. %%makelut  
  2. clc  
  3. clear  
  4.   
  5.   
  6. f=inline('sum(x(:))>=3');%inline是用来定义局部函数的  
  7. lut2=makelut(f,2)%为函数f构造一个接收2*2矩阵的查找表  
  8. lut3=makelut(f,3)  
  9.   
  10.   
  11. %% Conway生命游戏  
  12. clc  
  13. clear  
  14. lut=makelut(@conwaylaws,3);  
  15. bw1=  [0     0     0     0     0     0     0     0     0     0  
  16.        0     0     0     0     0     0     0     0     0     0  
  17.        0     0     0     1     0     0     1     0     0     0  
  18.        0     0     0     1     1     1     1     0     0     0  
  19.        0     0     1     0     0     0     0     1     0     0  
  20.        0     0     1     0     1     1     0     1     0     0  
  21.        0     0     1     0     0     0     0     1     0     0  
  22.        0     0     0     1     1     1     1     0     0     0  
  23.        0     0     0     0     0     0     0     0     0     0  
  24.        0     0     0     0     0     0     0     0     0     0  ];  
  25. subplot(221),imshow(bw1,'InitialMagnification','fit');  
  26. title('Generation 1');  
  27.   
  28.   
  29. bw2=applylut(bw1,lut);  
  30. subplot(222),imshow(bw2,'InitialMagnification','fit'),  
  31. title('Generation 2');  
  32.   
  33.   
  34. bw3=applylut(bw2,lut);  
  35. subplot(223),imshow(bw3,'InitialMagnification','fit');  
  36. title('Generation 3');  
  37.   
  38.   
  39. temp=bw1;  
  40. for i=2:100  
  41.     bw100=applylut(temp,lut);  
  42.     temp=bw100;  
  43. end  
  44. subplot(224),imshow(bw100,'InitialMagnification','fit')  
  45. title('Generation 100');  

显示Generation结果如下:



[html]  view plain  copy
  1. %% getsequence  
  2. clc  
  3. clear  
  4. se=strel('diamond',5)  
  5. decomp=getsequence(se)%getsequence函数为得到分解的strel序列  
  6. decomp(1)  
  7. decomp(2)  
  8.   
  9.   
  10. %% endpoints  
  11. clc  
  12. clear  
  13.   
  14.   
  15. f1=imread('.\images\dipum_images_ch09\Fig0914(a)(bone-skel).tif');  
  16. subplot(121),imshow(f1);  
  17. title('原始形态骨架图像');  
  18.   
  19.   
  20. g1=endpoints(f1);  
  21. %set(gcf,'outerposition',get(0,'screensize'));%运行完后自动生成最大的窗口  
  22. subplot(122),imshow(g1);  
  23. title('骨架图像的端点图像');  



骨架头像端点检测头像如下:



[html]  view plain  copy
  1. f2=imread('.\images\dipum_images_ch09\Fig0916(a)(bone).tif');  
  2. figure,subplot(121),imshow(f2);  
  3. title('原始骨头图像');  
  4.   
  5.   
  6. g2=endpoints(f2);  
  7. subplot(122),imshow(g2);  
  8. title('骨头图像端点头像');%结果是没有端点  


骨头头像端点检测图像如下:



[html]  view plain  copy
  1. %% bwmorph组合常见形态学之细化  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0911(a)(noisy-fingerprint).tif');  
  5. subplot(221),imshow(f);  
  6. title('指纹图像细化原图');  
  7.   
  8.   
  9. g1=bwmorph(f,'thin',1);  
  10. subplot(222),imshow(g1);  
  11. title('指纹图像细化原图');  
  12.   
  13.   
  14. g2=bwmorph(f,'thin',2);  
  15. subplot(223),imshow(g2);  
  16. title('指纹图像细化原图');  
  17.   
  18.   
  19. g3=bwmorph(f,'thin',Inf);  
  20. subplot(224),imshow(g3);  
  21. title('指纹图像细化原图');  



指纹图像细化过程显示如下:



[html]  view plain  copy
  1. %% bwmorph组合常见形态学之骨骼化  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0911(a)(noisy-fingerprint).tif');  
  5. subplot(131),imshow(f);  
  6. title('指纹图像骨骼化原图');  
  7.   
  8.   
  9. fs=bwmorph(f,'skel',Inf);  
  10. subplot(132),imshow(fs);  
  11. title('指纹图像骨骼化');  
  12.   
  13.   
  14. for k=1:5  
  15.     fs=fs&~endpoints(fs);  
  16. end  
  17. subplot(133),imshow(fs);  
  18. title('指纹图像修剪后骨骼话');  



指纹图像骨骼化过程显示:



[html]  view plain  copy
  1. %% 使用函数bwlabel标注连通分量  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0917(a)(ten-objects).tif');  
  5. imshow(f),title('标注连通分量原始图像');  


其结果显示如下:



[html]  view plain  copy
  1. [L,n]=bwlabel(f);%L为标记矩阵,n为找到连接分量的总数  
  2. [r,c]=find(L==3);%返回第3个对象所有像素的行索引和列索引  
  3.   
  4.   
  5. rbar=mean(r);  
  6. cbar=mean(c);  
  7.   
  8.   
  9. figure,imshow(f)  
  10. hold on%保持当前图像使其不被刷新  
  11. for k=1:n  
  12.     [r,c]=find(L==k);  
  13.     rbar=mean(r);  
  14.     cbar=mean(c);  
  15.     plot(cbar,rbar,'Marker','o','MarkerEdgeColor','k',...  
  16.          'MarkerFaceColor','k','MarkerSize',10);%这个plot函数用法不是很熟悉  
  17.     plot(cbar,rbar,'Marker','*','MarkerFaceColor','w');%其中的marker为标记  
  18. end  
  19. title('标记所有对象质心后的图像');  




[html]  view plain  copy
  1. %% 由重构做开运算  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0922(a)(book-text).tif');  
  5. subplot(321),imshow(f);  
  6. title('重构原始图像');  
  7.   
  8.   
  9. fe=imerode(f,ones(51,1));%竖线腐蚀  
  10. subplot(322),imshow(fe);  
  11. title('使用竖线腐蚀后的结果');  
  12.   
  13.   
  14. fo=imopen(f,ones(51,1));%竖线做开运算  
  15. subplot(323),imshow(fo);  
  16. title('使用竖线做开运算结果');  
  17.   
  18.   
  19. fobr=imreconstruct(fe,f);%fe做标记  
  20. subplot(324),imshow(fobr);  
  21. title('使用竖线做重构开运算');  
  22.   
  23.   
  24. ff=imfill(f,'holes');%对f进行孔洞填充  
  25. subplot(325),imshow(ff);  
  26. title('对f填充孔洞后的图像');  
  27.   
  28.   
  29. fc=imclearborder(f,8);%清除边界,2维8邻接  
  30. subplot(326),imshow(fc);  
  31. title('对f清除边界后的图像');  


图像重构过程显示如下:



[html]  view plain  copy
  1. %% 使用顶帽变换和底帽变换  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0926(a)(rice).tif');  
  5. subplot(221),imshow(f);  
  6. title('顶帽底帽变换原始图像');  
  7.   
  8.   
  9. se=strel('disk',10);%产生结构元素  
  10. %顶帽变换是指原始图像减去其开运算的图像  
  11. %而开运算可用于补偿不均匀的背景亮度,所以用一个大的结构元素做开运算后  
  12. %然后用原图像减去这个开运算,就得到了背景均衡的图像,这也叫做是图像的顶帽运算  
  13. f1=imtophat(f,se);%使用顶帽变换  
  14. subplot(222),imshow(f1);  
  15. title('使用顶帽变换后的图像');  
  16.   
  17.   
  18. %底帽变换是原始图像减去其闭运算后的图像  
  19. f2=imbothat(imcomplement(f),se);%使用底帽变换,为什么原图像要求补呢?  
  20. %f2=imbothat(f,se);%使用底帽变换  
  21. subplot(223),imshow(f2);  
  22. title('使用底帽变换后的图像');  
  23.   
  24.   
  25. %顶帽变换和底帽变换联合起来用,用于增加对比度  
  26. f3=imsubtract(imadd(f,imtophat(f,se)),imbothat(f,se));%里面参数好像不合理?  
  27. subplot(224),imshow(f3);  
  28. title('使用顶帽底帽联合变换后图像');  



顶帽底帽变换过程图像如下:



[html]  view plain  copy
  1. %%使用开运算和闭运算做形态学平滑  
  2. %由于开运算可以除去比结构元素更小的明亮细节,闭运算可以除去比结构元素更小的暗色细节  
  3. %所以它们经常组合起来一起进行平滑图像并去除噪声  
  4. clc  
  5. clear  
  6. f=imread('.\images\dipum_images_ch09\Fig0925(a)(dowels).tif');  
  7. subplot(221),imshow(f);  
  8. title('木钉图像原图');  
  9.   
  10.   
  11. se=strel('disk',5);%disk其实就是一个八边形  
  12. fo=imopen(f,se);%经过开运算  
  13. subplot(222),imshow(f);  
  14. title('使用半径5的disk开运算后的图像');  
  15.   
  16.   
  17. foc=imclose(fo,se);  
  18. subplot(223),imshow(foc);  
  19. title('先开后闭的图像');  
  20.   
  21.   
  22. fasf=f;  
  23. for i=2:5  
  24.     se=strel('disk',i);  
  25.     fasf=imclose(imopen(fasf,se),se);  
  26. end  
  27. subplot(224),imshow(fasf);  
  28. title('使用开闭交替滤波后图像');  


使用开运算和闭运算做形态学平滑结果如下:



[html]  view plain  copy
  1. %% 颗粒分析  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0925(a)(dowels).tif');  
  5.   
  6.   
  7. sumpixels=zeros(1,36);  
  8. for k=0:35  
  9.     se=strel('disk',k);  
  10.     fo=imopen(f,se);  
  11.     sumpixels(k+1)=sum(fo(:));  
  12. end  
  13.   
  14.   
  15. %可以看到,连续开运算之间的表面积会减少  
  16. plot(0:35,sumpixels),xlabel('k'),ylabel('surface area');  
  17. title('表面积和结构元素半径之间的关系');  


其运算结果如下:   




[html]  view plain  copy
  1. figure,plot(-diff(sumpixels));%diff()函数为差分或者近似倒数,即相邻2个之间的差值  
  2. xlabel('k'),ylabel('surface area reduction');  
  3. title('减少的表面积和结构元素半径之间的关系');  


其运算结果如下:



[html]  view plain  copy
  1. %% 使用重构删除复杂图像的背景  
  2. clc  
  3. clear  
  4. f=imread('.\images\dipum_images_ch09\Fig0930(a)(calculator).tif');  
  5. subplot(221),imshow(f);  
  6. title('灰度级重构原图像');  
  7.   
  8.   
  9. f_obr=imreconstruct(imerode(f,ones(1,71)),f);  
  10. subplot(222),imshow(f_obr);  
  11. title('经开运算重构图');  
  12.   
  13.   
  14. f_o=imopen(f,ones(1,71));  
  15. subplot(223),imshow(f_o);  
  16. title('经开运算后图');  
  17.   
  18.   
  19. f_thr=imsubtract(f,f_obr);  
  20. subplot(224),imshow(f_thr);  
  21. title('顶帽运算重构图')  



使用重构删除复杂图像的背景1:



[html]  view plain  copy
  1. f_th=imsubtract(f,f_o)  
  2. figure,subplot(221),imshow(f_th);  
  3. title('经顶帽运算图');  
  4.   
  5.   
  6. g_obr=imreconstruct(imerode(f_thr,ones(1,11)),f_thr);  
  7. subplot(222),imshow(g_obr);  
  8. title('用水平线对f_thr经开运算后重构图');  
  9.   
  10.   
  11. g_obrd=imdilate(g_obr,ones(1,2));  
  12. subplot(223),imshow(g_obrd);  
  13. title('使用水平线对上图进行膨胀');  
  14.   
  15.   
  16. f2=imreconstruct(min(g_obrd,f_thr),f_thr);  
  17. subplot(224),imshow(f2);  
  18. title('最后的重构结果');  



使用重构删除复杂图像的背景2:



  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值