• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Matlab DIP(瓦)ch10图像分割练习

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

    这一章中主要是用数字图像处理技术对图像进行分割。因为图像分割是个比较难的课题。这里练习的是比较基本的。包过点、线和边缘的检测,hough变换的应用,阈值处理,基于区域的分割以及基于分水岭方法的分割。

   其练习代码和结果如下:

  1 %% 图像分割
2
3 %% 点检测
4 clc
5 clear
6 f=imread(\'.\images\dipum_images_ch10\Fig1002(a)(test_pattern_with_single_pixel).tif\');
7 subplot(121),imshow(f),title(\'点检测原图\');
8
9 w=[-1,-1,-1;
10 -1,8,-1;
11 -1,-1,-1]
12 g=abs(imfilter(double(f),w));
13 subplot(122),imshow(g),title(\'点检测结果\');
14%点检测图结果如下:


15
16 %% 非线性滤波点检测
17 clc
18 clear
19 f=imread(\'.\images\dipum_images_ch10\Fig1002(a)(test_pattern_with_single_pixel).tif\');
20 subplot(121),imshow(f),title(\'非线性滤波点检测原图\');
21
22 m=3;
23 n=3;
24 %ordfilt2(f,order,domin)表示的是用矩阵domin中为1的地方进行排序,然后选择地order个位置的值代替f中的值,属于非线性滤波
25 g=imsubtract(ordfilt2(f,m*n,ones(m,n)),ordfilt2(f,1,ones(m,n)));%滤波邻域的最大值减去最小值
26 T=max(g(:));
27 g2=g>=T;
28 subplot(122),imshow(g2);
29 title(\'非线性滤波点检测后图\');
30%运行结果如下:


31
32 %% 检测指定方向的线
33 clc
34 clear
35 f=imread(\'.\images\dipum_images_ch10\Fig1004(a)(wirebond_mask).tif\');
36 subplot(321),imshow(f);
37 title(\'检测指定方向线的原始图像\');
38
39 w=[2 -1 -1;
40 -1 2 -1;
41 -1 -1 2];%矩阵用逗号或者空格隔开的效果是一样的
42 g=imfilter(double(f),w);
43 subplot(322),imshow(g,[]);
44 title(\'使用-45度检测器处理后的图像\');
45
46 gtop=g(1:120,1:120);%取g的左上角图
47 gtop=pixeldup(gtop,4);%扩大4*4倍的图
48 subplot(323),imshow(gtop,[]);
49 title(\'-45度检测后左上角放大图\');
50
51 gbot=g(end-119:end,end-119:end);%取右下角图
52 gbot=pixeldup(gbot,4);%扩大16倍的图
53 subplot(324),imshow(gbot,[]);
54 title(\'-45度检测后右下角后放大图\');
55
56 g=abs(g);
57 subplot(325),imshow(g,[]);
58 title(\'-45度检测后的绝对值图\');
59
60 T=max(g(:));
61 g=g>=T;
62 subplot(326),imshow(g);
63 title(\'-45度检测后取绝对值最大的图\')
64%检测指定方向的线过程如下:


65
66 %% sobel检测器检测边缘
67 clc
68 clear
69 f=imread(\'.\images\dipum_images_ch10\Fig1006(a)(building).tif\');
70 subplot(321),imshow(f);
71 title(\'sobel检测的原始图像\');
72
73 [gv,t]=edge(f,\'sobel\',\'vertical\');%斜线因为具有垂直分量,所以也能够被检测出来
74 subplot(322),imshow(gv);
75 title(\'sobel垂直方向检测后图像\');
76
77 gv=edge(f,\'sobel\',0.15,\'vertical\');
78 subplot(323),imshow(gv);
79 title(\'sobel垂直检测0.15阈值后图像\');
80
81 gboth=edge(f,\'sobel\',0.15);
82 subplot(324),imshow(gboth);
83 title(\'sobel水平垂直方向阈值0.15后图像\');
84
85 w45=[-2 -1 0
86 -1 0 1
87 0 1 2];%相当于45度的sobel检测算子
88 g45=imfilter(double(f),w45,\'replicate\');
89 T=0.3*max(abs(g45(:)));
90 g45=g45>=T;
91 subplot(325),imshow(g45);
92 title(\'sobel正45度方向上检测图\');
93
94 w_45=[0 -1 -2
95 1 0 -1
96 2 1 0];
97 g_45=imfilter(double(f),w_45,\'replicate\');
98 T=0.3*max(abs(g_45(:)));
99 g_45=g_45>=T;
100 subplot(326),imshow(g_45);
101 title(\'sobel负45度方向上检测图\');
102%sobel检测过程如下:


103
104 %% sobel,log,canny边缘检测器的比较
105 clc
106 clear
107 f=imread(\'.\images\dipum_images_ch10\Fig1006(a)(building).tif\');
108
109 [g_sobel_default,ts]=edge(f,\'sobel\');%
110 subplot(231),imshow(g_sobel_default);
111 title(\'g sobel default\');
112
113 [g_log_default,tlog]=edge(f,\'log\');
114 subplot(233),imshow(g_log_default);
115 title(\'g log default\');
116
117 [g_canny_default,tc]=edge(f,\'canny\');
118 subplot(235),imshow(g_canny_default);
119 title(\'g canny default\');
120
121 g_sobel_best=edge(f,\'sobel\',0.05);
122 subplot(232),imshow(g_sobel_best);
123 title(\'g sobel best\');
124
125 g_log_best=edge(f,\'log\',0.003,2.25);
126 subplot(234),imshow(g_log_best);
127 title(\'g log best\');
128
129 g_canny_best=edge(f,\'canny\',[0.04 0.10],1.5);
130 subplot(236),imshow(g_canny_best);
131 title(\'g canny best\');
132%3者比较的结果如下:


133
134 %% hough变换说明
135 clc
136 clear
137 f=zeros(101,101);
138 f(1,1)=1;
139 f(101,1)=1;
140 f(1,101)=1;
141 f(51,51)=1;
142 f(101,101)=1;
143 imshow(f);title(\'带有5个点的二值图像\');
144%显示如下:


145
146 H=hough(f);
147 figure,imshow(H,[]);
148 title(\'不带标度的hough变换\');
149%不带标度的hough变换结果如下:


150
151 [H,theta,rho]=hough(f);
152 figure,imshow(theta,rho,H,[],\'notruesize\');%为什么显示不出来呢
153 axis on,axis normal;
154 xlabel(\'\theta\'),ylabel(\'\rho\');
155
156 %% 计算全局阈值
157 clc
158 clear
159 f = imread(\'.\images\dipum_images_ch10\Fig1013(a)(scanned-text-grayscale).tif\');
160 imshow(f);
161 title(\'全局阈值原始图像\')
162%其图片显示结果如下:


163
164 T=0.5*(double(min(f(:)))+double(max(f(:))));
165 done=false;
166 while ~done
167 g=f>=T;
168 Tnext=0.5*(mean(f(g))+mean(f(~g)));
169 done=abs(T-Tnext)<0.5
170 T=Tnext;
171 end
172 g=f<=T;%因为前景是黑色的字,所以要分离出来的话这里就要用<=.
173 figure,subplot(121),imshow(g);
174 title(\'使用迭代方法得到的阈值处理图像\');
175
176
177 T2=graythresh(f);%得到的是0~1的小数?
178 g=f<=T2*255;
179 subplot(122),imshow(g);
180 title(\'使用函数graythresh得到的阈值处理图像\');
181%阈值处理后结果如下:


182
183 %% 焊接空隙区域生长
184 clc
185 clear
186 f = imread(\'.\images\dipum_images_ch10\Fig1014(a)(defective_weld).tif\');
187 subplot(221),imshow(f);
188 title(\'焊接空隙原始图像\');
189
190 %函数regiongrow返回的NR为是不同区域的数目,参数SI是一副含有种子点的图像
191 %TI是包含在经过连通前通过阈值测试的像素
192 [g,NR,SI,TI]=regiongrow(f,255,65);%种子的像素值为255,65为阈值
193
194 subplot(222),imshow(SI);
195 title(\'焊接空隙种子点的图像\');
196
197 subplot(223),imshow(TI);
198 title(\'焊接空隙所有通过阈值测试的像素\');
199
200 subplot(224),imshow(g);
201 title(\'对种子点进行8连通分析后的结果\');
202%焊接空隙区域生长图如下:


203
204 %% 使用区域分离和合并的图像分割
205 clc
206 clear
207 f = imread(\'.\images\dipum_images_ch10\Fig1017(a)(cygnusloop_Xray_original).tif\');
208 subplot(231),imshow(f);
209 title(\'区域分割原始图像\');
210
211 g32=splitmerge(f,32,@predicate);%32代表分割中允许最小的块
212 subplot(232),imshow(g32);
213 title(\'mindim为32时的分割图像\');
214
215 g16=splitmerge(f,16,@predicate);%32代表分割中允许最小的块
216 subplot(233),imshow(g16);
217 title(\'mindim为32时的分割图像\');
218
219 g8=splitmerge(f,8,@predicate);%32代表分割中允许最小的块
220 subplot(234),imshow(g8);
221 title(\'mindim为32时的分割图像\');
222
223 g4=splitmerge(f,4,@predicate);%32代表分割中允许最小的块
224 subplot(235),imshow(g4);
225 title(\'mindim为32时的分割图像\');
226
227 g2=splitmerge(f,2,@predict);%32代表分割中允许最小的块
228 subplot(236),imshow(g2);
229 title(\'mindim为32时的分割图像\');
230
231 %% 使用距离和分水岭变换分割灰度图像
232 clc
233 clear
234 f = imread(\'.\images\dipum_images_ch10\Fig0925(a)(dowels).tif\');
235 subplot(231),imshow(f);title(\'使用距离和分水岭分割原图\');
236
237 g=im2bw(f,graythresh(f));
238 subplot(232),imshow(g),title(\'原图像阈值处理后的图像\');
239
240 gc=~g;
241 subplot(233),imshow(gc),title(\'阈值处理后取反图像\');
242
243 D=bwdist(gc);
244 subplot(234),imshow(D),title(\'使用距离变换后的图像\');
245
246 L=watershed(-D);
247 w=L==0;
248 subplot(235),imshow(w),title(\'距离变换后的负分水岭图像\');
249
250 g2=g & ~w;
251 subplot(236),imshow(g2),title(\'阈值图像与分水岭图像相与图像\');
252%使用距离分水岭图像如下:


253
254 %% 使用梯度和分水岭变换分割灰度图像
255 clc
256 clear
257 f = imread(\'.\images\dipum_images_ch10\Fig1021(a)(small-blobs).tif\');
258 subplot(221),imshow(f);
259 title(\'使用梯度和分水岭变换分割灰度图像\');
260
261 h=fspecial(\'sobel\');
262 fd=double(f);
263 g=sqrt(imfilter(fd,h,\'replicate\').^2+imfilter(fd,h\',\'replicate\').^2);
264 subplot(222),imshow(g,[]);
265 title(\'使用梯度和分水岭分割幅度图像\');
266
267 L=watershed(g);
268 wr=L==0;
269 subplot(223),imshow(wr);
270 title(\'对梯度复制图像进行二值分水岭后图像\');
271
272 g2=imclose(imopen(g,ones(3,3)),ones(3,3));
273 L2=watershed(g2);
274 wr2=L2==0;
275 f2=f;
276 f2(wr2)=255;
277 subplot(224),imshow(f2);
278 title(\'平滑梯度图像后的分水岭变换\');
279%使用梯度和分水岭变换分割灰度图像结果如下:


280
281 %% 控制标记符的分水岭分割
282 clc
283 clear
284 f = imread(\'.\images\dipum_images_ch10\Fig1022(a)(gel-image).tif\');
285 imshow(f);
286 title(\'控制标记符的分水岭分割原图像\');


287
288 h=fspecial(\'sobel\');
289 fd=double(f);
290 g=sqrt(imfilter(fd,h,\'replicate\').^2+imfilter(fd,h\',\'replicate\').^2);
291 L=watershed(g);
292 wr=L==0;
293 figure,subplot(231),imshow(wr,[]);
294 title(\'控制标记符的分水岭分割幅度图像\');
295
296 rm=imregionalmin(g);%梯度图像有很多较浅的坑,造成的原因是原图像不均匀背景中灰度细小的变化
297 subplot(232),imshow(rm,[]);
298 title(\'对梯度幅度图像的局部最小区域\');
299
300 im=imextendedmin(f,2);%得到内部标记符
301 fim=f;
302 fim(im)=175;
303 subplot(233),imshow(f,[]);
304 title(\'内部标记符\');
305
306 Lim=watershed(bwdist(im));
307 em=Lim==0;
308 subplot(234),imshow(em,[]);
309 title(\'外部标记符\');
310
311 g2=imimposemin(g,im | em);
312 subplot(235),imshow(g2,[]);
313 title(\'修改后的梯度幅度值\');
314
315 L2=watershed(g2);
316 f2=f;
317 f2(L2==0)=255;
318 subplot(236),imshow(f2),title(\'最后分割的结果\');
319%控制标记符的分水岭分割过程如下:

 

   图像分割中处理涉及到很多参数都需要自己根据具体问题不断调试,直到选出最好的参数组合为止。

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi2010新增功能之:TWICImage类[1]-方便的图像格式转换发布时间:2022-07-18
下一篇:
Delphi常用关键字用法详解发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap