Matlab DIP(瓦)ch11表示與描述練習


     這一章主要是練習一些圖像的表示和描述,內容主要包括圖像的一些常用表示方法,比如說鏈碼,MPP,標記,邊界片段,骨骼等。另一方面是圖像的描述算子,比如說邊界描述算子,區域描述算子等。有些課本練習的代碼暫時還沒有搞定。

     其練習過程和結果如下所示:

  1 %%  數組單元概念
2 clc
3 clear
4 f=imread('..\images\dipum_images_ch11\Fig1102(a)(noisy_circular_stroke).tif');
5 imshow(f);
6 [a b c d]=image_stats(f);
7 a=image_stats1(f);
8 b=image_stats2(f);
9
10 %% Freeman鏈碼及其某些變體
11 clc
12 clear
13 f=imread('..\images\dipum_images_ch11\Fig1102(a)(noisy_circular_stroke).tif');
14 subplot(231),imshow(f);
15 title('freeman原圖');
16
17 h=fspecial('average',9);%9*9平滑算子
18 g=imfilter(f,h,'replicate');%濾波
19 subplot(232),imshow(g);
20 title('freeman中值濾波后圖像');
21
22 g=im2bw(g,0.5);%將原圖像轉換為二值圖像
23 subplot(233),imshow(g);
24 title('freeman圖像閾值處理后');
25
26 %函數boundaries(BW,CONN,DIR)表示的是跟蹤目標的邊界,返回值為一個p*1的數組單元,p為目標的個數
27 %每一個單元又是一個Q*2的矩陣,即Q個點的x,y坐標
28 B=boundaries(g);
29
30 %函數cellfun(FUN,C)是指對一個單元數組中的每一個單元應用函數FUN
31 d=cellfun('length',B);%求B中每一個目標邊界的長度,所以返回值d是一個向量
32
33 [max_d,k]=max(d);%返回向量d中最大的值,存在max_d中,k為其索引。
34 v=B{k(1)};%如果最大邊界不止一條,則取出其中的一條即可。v是一個坐標數組
35 [M,N]=size(g);
36
37 %函數bound2im(b,M,N,x0,y0)是生成一副二值圖像,大小為M*N,x0和y0是b中最小的x和y軸坐標
38 g=bound2im(v,M,N,min(v(:,1)),min(v(:,2)));
39
40 subplot(234),imshow(g);
41 title('取出最大邊界后圖像');
42
43 % [S, SU] = bsubsamp(B, GRIDSEP)指的是對邊界B進行子采樣,每個GRIDSEP個點采樣一次
44 %輸出S是采樣后的值,輸出SU是歸一化的邊界,什么意思?
45 [s,su]=bsubsamp(v,50);
46 g2=bound2im(s,M,N,min(s(:,1)),min(s(:,2)));
47 subplot(235),imshow(g2);
48 title('蔣采樣后的邊界圖像');
49
50 %connectpoly(X,Y)函數是按照坐標(X,Y)順時針或者逆時針連接成多邊形
51 cn=connectpoly(s(:,1),s(:,2));
52 g2=bound2im(cn,M,N,min(cn(:,1)),min(cn(:,2)));
53 subplot(236),imshow(g2);
54 title('連接成多邊形圖像');
55%freeman鏈碼過程如下:


56
57 %函數fchcode為計算邊界的freeman鏈碼
58 c=fchcode(su);
59%鏈碼結果:


60
61 %% 獲得一個區域的邊界的細胞牆(木有搞定)
62 clc
63 clear
64 f=imread('..\images\dipum_images_ch11\Fig1107(a)(mapleleaf).tif');
65 subplot(231),imshow(f);
66 title('原圖像');
67
68 B=bwperim(f,8);%返回一個包含二進制圖像的邊界圖像,采用4近鄰
69 subplot(232),imshow(B);
70 title('二進制邊界圖像');
71
72 Q=qtdecomp(B,0,2);%對B圖進行四叉樹分解,最小尺寸為2*2,用0做閾值
73 subplot(233),imshow(Q);
74 title('四叉樹分解后圖像')
75
76 %% 使用函數minperpoly
77 clc
78 clear
79 f=imread('..\images\dipum_images_ch11\Fig1107(a)(mapleleaf).tif');
80 subplot(231),imshow(f);
81 title('原圖像');
82
83 b=boundaries(f,4,'cw');%找出B的邊界,順時針4鄰接
84 b=b{1};%因為這幅圖比較特殊,它只有一個邊界物體
85 [M,N]=size(f);
86 xmin=min(b(:,1));
87 ymin=min(b(:,2));
88 bim=bound2im(b,M,N,xmin,ymin);%將邊界物體轉換成邊界圖像
89 subplot(232),imshow(bim);
90 title('找出的邊界圖像');
91
92 % [X, Y] = minperpoly(F, CELLSIZE)為計算二值圖像的最小周長多邊形
93 [x,y]=minperpoly(f,2)%2為用於形成邊界方形的大小,x和y是MPP上頂點的坐標
94
95 b2=connectpoly(x,y);%將頂點依次連接成多邊形
96 b2=bound2im(b2,M,N,xmin,ymin);
97 subplot(233),imshow(b2);
98 title('使用大小為2*2的方形邊界單元獲得的MPP');
99
100 [x,y]=minperpoly(f,3)%2為用於形成邊界方形的大小,x和y是MPP上頂點的坐標
101 b3=connectpoly(x,y);%將頂點依次連接成多邊形
102 b3=bound2im(b3,M,N,xmin,ymin);
103 subplot(234),imshow(b3);
104 title('使用大小為3*3的方形邊界單元獲得的MPP');
105
106 [x,y]=minperpoly(f,4)%2為用於形成邊界方形的大小,x和y是MPP上頂點的坐標
107 b4=connectpoly(x,y);%將頂點依次連接成多邊形
108 b4=bound2im(b4,M,N,xmin,ymin);
109 subplot(235),imshow(b4);
110 title('使用大小為4*4的方形邊界單元獲得的MPP');
111
112 [x,y]=minperpoly(f,8)%2為用於形成邊界方形的大小,x和y是MPP上頂點的坐標
113 b8=connectpoly(x,y);%將頂點依次連接成多邊形
114 b8=bound2im(b8,M,N,xmin,ymin);
115 subplot(236),imshow(b8);
116 title('使用大小為8*8的方形邊界單元獲得的MPP');
117%使用minperpoly過程如下:


118
119 %% 標記(木有搞定)
120 clc
121 clear
122 f=imread('..\images\dipum_images_ch11\Fig1111(a)(boundary_sq.tif');
123 imshow(f);
124 title('標記原圖');
125
126 [st,angle,x0,y0]=signature(f);
127 plot(angle,st)
128
129 %% 計算一個區域的骨骼
130 clc
131 clear
132 f=imread('..\images\dipum_images_ch11\Fig1113(a)(chromo_original).tif');
133 subplot(231),imshow(f);
134 whos f;%其實是個二值圖像
135 title('人體染色體原始圖');
136
137 f=im2double(f);%不再是二值圖像了
138 h=fspecial('gaussian',25,25);
139 g=imfilter(f,h,'replicate');
140 subplot(232),imshow(g);
141 title('高斯平滑后的圖像');%為什么平滑后那些雜點就沒了呢,至少平滑到那點時像素值它自己的權值最大的啊?
142
143 g=im2bw(g,1.5*graythresh(g));%其中的graythresh()函數為自動閾值,處理后g為二值圖像了
144 subplot(233),imshow(g);
145 title('經閾值處理后');
146
147 s=bwmorph(g,'skel',Inf);%提取骨骼,bwmorph為二進制形態學處理函數
148 subplot(234),imshow(s);
149 title('骨骼化后圖像');
150
151 s1=bwmorph(s,'spur',8);%刪除毛刺
152 subplot(235),imshow(s1);
153 title('8次去毛刺后');
154
155 s2=bwmorph(s1,'spur',7);%刪除毛刺
156 subplot(236),imshow(s2);
157 title('再7次去毛刺后');
158%提取染色體骨骼圖過程如下:


159
160 %% 傅里葉描述子
161 clc
162 clear
163 f=imread('..\images\dipum_images_ch11\Fig1113(a)(chromo_original).tif');
164 subplot(331),imshow(f);
165 whos f;%其實是個二值圖像
166 title('人體染色體原始圖');
167
168 f=im2double(f);%不再是二值圖像了
169 h=fspecial('gaussian',15,9);
170 g=imfilter(f,h,'replicate');
171 g=im2bw(g,0.7);%其中的graythresh()函數為自動閾值,處理后g為二值圖像了
172 subplot(332),imshow(g);
173 title('經閾值處理后');
174
175 b=boundaries(g);
176 b=b{1};
177 bim=bound2im(b,344,270);%344,270為圖片的尺寸
178 subplot(333),imshow(bim);
179 title('提取的邊界圖');
180
181 z=frdescp(b);%對邊界坐標b進行傅里葉變換系數(有多少個點就有多少個系數),將b的坐標點看成是復平面中的某個復數
182 z14=ifrdescp(z,546);%用50%的描述子進行逆變換
183 z546im=bound2im(z14,344,270);
184 subplot(334),imshow(z546im);
185 title('546個描述子恢復后');
186
187 z110=ifrdescp(z,110);%用50%的描述子進行逆變換
188 z110im=bound2im(z110,344,270);
189 subplot(335),imshow(z110im);
190 title('110個描述子恢復后');
191
192 z56=ifrdescp(z,56);%用50%的描述子進行逆變換
193 z56im=bound2im(z56,344,270);
194 subplot(336),imshow(z56im);
195 title('56個描述子恢復后');
196
197 z28=ifrdescp(z,28);%用50%的描述子進行逆變換
198 z28im=bound2im(z28,344,270);
199 subplot(337),imshow(z28im);
200 title('28個描述子恢復后');
201
202 z14=ifrdescp(z,14);%用50%的描述子進行逆變換
203 z14im=bound2im(z14,344,270);
204 subplot(338),imshow(z14im);
205 title('14個描述子恢復后');
206
207 z8=ifrdescp(z,8);%用50%的描述子進行逆變換
208 z8im=bound2im(z8,344,270);
209 subplot(339),imshow(z8im);
210 title('8個描述子恢復后');
211%傅里葉描述子圖過程:


212
213 %% 函數regionprops的使用
214 clc
215 clear
216 B = [1 1 1 0 0 0 0 0
217 1 1 1 0 1 1 0 0
218 1 1 1 0 1 1 0 0
219 1 1 1 0 0 0 0 0
220 1 1 1 0 0 0 1 0
221 1 1 1 0 0 0 1 0
222 1 1 1 0 0 1 1 0
223 1 1 1 0 0 0 0 0];
224 B=bwlabel(B);%將矩陣B變成標記矩陣,默認為8連通,將連通區域用不同的整數給標記出來
225
226 D=regionprops(B,'area','boundingbox')
227 w=[D.Area]
228 NR=length(w)
229
230 V=cat(1,D.BoundingBox)%cat函數表示沿着第一個方向級聯存放數組
231%該函數的使用過程如下:


232
233 %% 統計紋理度量(木有搞定)
234 clc
235 clear
236 f=imread('..\images\dipum_images_ch11\Fig1119(a)(superconductor-with-window).tif');
237 sA=imhist(f);
238 statxture(sA)%計算圖像的6個統計度量量
239
240 %% 計算頻譜紋理
241 clc
242 clear
243
244 f1=imread('..\images\dipum_images_ch11\Fig1121(a)(random_matches).tif');
245 subplot(241),imshow(f1);
246 title('隨機排列火柴');
247
248 f2=imread('..\images\dipum_images_ch11\Fig1121(b)(ordered_matches).tif');
249 subplot(245),imshow(f2);
250 title('規則排列火柴');
251
252 [srad1,sang1,s1]=specxture(f1);
253 subplot(242),imshow(s1);
254 title('隨機排列火柴頻譜圖');
255
256 subplot(243),plot(srad1);
257 title('隨機排列火柴幅度圖');
258
259 subplot(244),plot(sang1);
260 title('隨機排列火柴角度圖');
261
262 [srad2,sang2,s2]=specxture(f2);
263 subplot(246),imshow(s2);
264 title('規則排列火柴頻譜圖');
265
266 subplot(247),plot(srad2);
267 title('規則排列火柴幅度圖');
268
269 subplot(248),plot(sang2);
270 title('規則排列火柴角度圖');
271%計算頻譜紋理圖過程如下:


272
273 %% 不變矩
274 clc
275 clear
276 f=imread('..\images\dipum_images_ch11\Fig1123(a)(Original_Padded_to_568_by_568).tif');
277 imshow(f);
278 title('不變矩原圖');
279%不變矩原始圖像如下:


280
281 %padarray函數為矩陣0填充,圖像原始大小為400*400,但是需要旋轉45度,所以最大變為568*568
282 fp=padarray(f,[84 84],'both');%因此四個方向每個方向都要擴充84個像素
283
284 fhs=f(1:2:end,1:2:end);%表示從第一個到最后一個,每2個采樣一次
285 figure,subplot(221),imshow(fhs);
286 title('半大圖像');
287 fhsp=padarray(fhs,[184 184],'both');
288
289 fm=fliplr(f);
290 subplot(222),imshow(fm);
291 title('鏡面圖像');
292 fmp=padarray(fm,[84 84],'both');
293
294 fr2=imrotate(f,2,'bilinear');%旋轉2度,雙線性插值
295 subplot(223),imshow(fr2);
296 title('旋轉2度后圖像');
297
298 fr45=imrotate(f,45,'bilinear');
299 subplot(224),imshow(fr45);
300 title('旋轉45度后圖像');
301%不變矩變換圖像如下:


302
303 phiorig=abs(log(invmoments(f)))
304 phihalf=abs(log(invmoments(fhs)))
305 phimirror=abs(log(invmoments(fm)))
306 phirot2=abs(log(invmoments(fr2)))
307 phirot45=abs(log(invmoments(fr45)))
308%這5幅圖像的7個不變矩圖像如下:


309
310 %圖像主分量
311 clc
312 clear
313 f1=imread('..\images\dipum_images_ch11\Fig1125(a)(WashingtonDC_Band1_512).tif');
314 subplot(231),imshow(f1);
315 title('可見藍色波段圖像');
316
317 f2=imread('..\images\dipum_images_ch11\Fig1125(b)(WashingtonDC_Band2_512).tif');
318 subplot(232),imshow(f2);
319 title('可見綠色波段圖像');
320
321 f3=imread('..\images\dipum_images_ch11\Fig1125(c)(WashingtonDC_Band3_512).tif');
322 subplot(233),imshow(f3);
323 title('可見紅色波段圖像');
324
325 f4=imread('..\images\dipum_images_ch11\Fig1125(d)(WashingtonDC_Band4_512).tif');
326 subplot(234),imshow(f4);
327 title('近紅外光圖像');
328
329 f5=imread('..\images\dipum_images_ch11\Fig1125(e)(WashingtonDC_Band5_512).tif');
330 subplot(235),imshow(f5);
331 title('中紅外光圖像');
332
333 f6=imread('..\images\dipum_images_ch11\Fig1125(f)(WashingtonDC_Band6_512).tif');
334 subplot(236),imshow(f6);
335 title('熱紅外光圖像');
336%6幅多譜圖像顯示結果如下:


337
338 S=cat(3,f1,f2,f3,f4,f5,f6);%將6幅圖像在第三維中連接起來
339 [X,R]=imstack2vectors(S);%將S組織到數組X中
340 p=princomp(X,6);%princomp用來獲得6幅圖像的主分量
341
342 g1=p.Y(:,1);%計算第1主維分量
343 g1=reshape(g1,512,512);
344 figure,subplot(231),imshow(g1,[]);
345
346 g2=p.Y(:,2);%計算第2主維分量
347 g2=reshape(g2,512,512);
348 subplot(232),imshow(g2,[]);
349
350 g3=p.Y(:,3);%計算第3主維分量
351 g3=reshape(g3,512,512);
352 subplot(233),imshow(g3,[]);
353
354 g4=p.Y(:,4);%計算第4主維分量
355 g4=reshape(g4,512,512);
356 subplot(234),imshow(g4,[]);
357
358 g5=p.Y(:,5);%計算第5主維分量
359 g5=reshape(g5,512,512);
360 subplot(235),imshow(g5,[]);
361
362 g6=p.Y(:,6);%計算第6主維分量
363 g6=reshape(g6,512,512);
364 subplot(236),imshow(g6,[]);
365%主分量分解過程如下:

 

     圖像的表示和描述對后面做計算機視覺處理起來很大的作用。當然針對不同的研究對象其表示和描述的特點也不同。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM