圖像細化(骨架化)算法 分析
圖像的細化是模式識別中很重要的一個技術,指的是將原本"臃腫"的像素簡化為單像素相連接的二值圖像(即類似骨架的概念),細化的好壞直接影響到後面識別匹配的效率。
摘自某文章的話,細化就是經過一層層的剝離,從原來的圖中去掉一些點,但仍要保持原來的形狀,直到得到圖像的骨架。骨架,可以理解為圖象的中軸,例如一個長方形的骨架是它的長方向上的中軸線;正方形的骨架是它的中心點;圓的骨架是它的圓心,直線的骨架是它自身,孤立點的骨架也是自身。
下面先介紹經典的Zhang並行快速細化算法:
設p1點的八鄰域為:
【 p9 p2 p3
p8 p1 p4
p7 p6 p5 】
(其中p1為白點,如果以下四個條件同時滿足,則刪除p1,即令p1=0)
其中迭代分為兩個子過程:
過程1 細化刪除條件為: (1)、2 < =N(p1) <= 6, N(x)為x的8鄰域中黑點的數目
(2)、A(p1)=1, A(x)指的是將p2-p8之間按序前後分別為0、1的對數(背景色:0)
(3)、p2*p4*p6=0
(4)、p4*p6*p8=0
如果同時滿足以上四個條件則該點可以刪除(賦值為0)。
過程2 細化刪除條件為: (1)、2 < =N(p1) <= 6, N(x)為x的8鄰域中黑點的數目
(2)、A(p1)=1, A(x)指的是將p2-p8之間按序前後分別為0、1的對數(背景色:0)
(3)、p2*p4*p8=0
(4)、p2*p6*p8=0
如果同時滿足以上四個條件則該點可以刪除。
代碼如下:
A.m
1 function n=A(temp,i,j) 2 %0->1的數目 3 shuzu=[temp(i,j),temp(i-1,j),temp(i-1,j+1),temp(i,j+1),temp(i+1,j+1),temp(i+1,j),temp(i+1,j-1),temp(i,j-1),temp(i-1,j-1)]; 4 n=0; 5 for i=2:8 6 if shuzu(i)==0&&shuzu(i+1)==1 7 n=n+1; 8 end 9 end
主函數代碼:
1 test=input('Please input a digits image:','s'); %輸入圖像 2 x=imread(test); 3 if ~isbw(x) 4 '請確保輸入圖像為二值化圖像!'; 5 else 6 [height,width]=size(x); 7 mark=1; 8 % temp=zeros(height+2,width+2); 9 % temp(2:height+1,2:width+1)=x(:,:); 10 temp=x; 11 imshow(temp); 12 while mark==1 13 mark=0; 14 15 for i=2:height-1 16 for j=2:width-1 17 condition=0; 18 %判斷P(r,c)是否為可細化像素 19 if temp(i,j)==1 20 n=0; 21 for ii=-1:1 22 for jj=-1:1 23 n=n+temp(i+ii,j+jj); 24 end 25 end 26 if (n>=3 && n<=7) 27 condition=condition+1; 28 end 29 if A(temp,i,j)==1 30 condition=condition+1; 31 end 32 if temp(i-1,j)*temp(i,j+1)*temp(i+1,j)==0 33 condition=condition+1; 34 end 35 if temp(i,j+1)*temp(i+1,j)*temp(i,j-1)==0 36 condition=condition+1; 37 end 38 if condition==4 39 mark=1; 40 temp(i,j)=0; 41 end 42 end 43 end 44 end 45 figure;imshow(temp); 46 47 48 for i=2:height-1 49 for j=2:width-1 50 condition=0; 51 %判斷P(r,c)是否為可細化像素 52 if temp(i,j)==1 53 n=0; 54 for ii=-1:1 55 for jj=-1:1 56 n=n+temp(i+ii,j+jj); 57 end 58 end 59 if (n>=3 && n<=7) 60 condition=condition+1; 61 end 62 if A(temp,i,j)==1 63 condition=condition+1; 64 end 65 if temp(i-1,j)*temp(i,j+1)*temp(i,j-1)==0 66 condition=condition+1; 67 end 68 if temp(i,j-1)*temp(i+1,j)*temp(i,j-1)==0 69 condition=condition+1; 70 end 71 if condition==4 72 mark=1; 73 temp(i,j)=0; 74 end 75 end 76 end 77 end 78 figure;imshow(temp); 79 end 80 end
結果:
留言列表