close

http://blog.csdn.net/aofengdaxia/article/details/7439992

上理論:

Hilditch 細化算法的步驟為:

對圖像從左向右從上向下迭代每個像素,是為一個迭代周期。在每個迭代周期中,對於每一個像素p,如果它同時滿足6個條件,則標記它。在當前迭代周期結束時,則把所有標記的像素的值設為背景值。如果某次迭代周期中不存在標記點(即滿足6個條件的像素),則算法結束。假設背景值為0,前景值為1,則:

6個條件為:

(I):p 為1,即p不是背景;

(2):x1,x3,x5,x7不全部為1(否則把p標記刪除,圖像空心了);

(3):x1~x8 中,至少有2個為1(若只有1個為1,則是線段的端點。若沒有為1的,則為孤立點);

(4):p的8連通聯結數為1;

聯結數指在像素p的3*3鄰域中,和p連接的圖形分量的個數:

(5)假設x3已經標記刪除,那麼當x3為0時,p的8聯通聯結數為1;

(6)假設x5已經標記刪除,那麼當x5為0時,p的8聯通聯結數為1。

以上的理論選擇網絡博客:http://www.cnblogs.com/xiaotie/archive/2010/08/12/1797760.html (此博客上有C#版本的Hilditch細化算法,但是使用unsafe的代碼不是太好理解的。

我上一個自己寫的代碼

[csharp] view plain copy
  1. /// <summary>  
  2.         /// Hilditch細化算法  
  3.         /// </summary>  
  4.         /// <param name="input"></param>  
  5.         /// <returns></returns>  
  6.         private int[,] ThinnerHilditch(int[,] input)  
  7.         {  
  8.             int lWidth = input.GetLength(0);  
  9.             int lHeight = input.GetLength(1);  
  10.   
  11.             bool IsModified = true;  
  12.             int Counter = 1;  
  13.             int[] nnb = new int[9];  
  14.             //去掉邊框像素  
  15.             for (int i = 0; i < lWidth; i++)  
  16.             {  
  17.                 input[i, 0] = 0;  
  18.                 input[i, lHeight - 1] = 0;  
  19.             }  
  20.             for (int j = 0; j < lHeight; j++)  
  21.             {  
  22.                 input[0, j] = 0;  
  23.                 input[lWidth - 1, j] = 0;  
  24.             }  
  25.             do  
  26.             {  
  27.                 Counter++;  
  28.                 IsModified = false;  
  29.                 int[,] nb = new int[3, 3];  
  30.                 for (int i = 1; i < lWidth; i++)  
  31.                 {  
  32.                     for (int j = 1; j < lHeight; j++)  
  33.                     {  
  34.                         //條件1必須為黑點  
  35.                         if (input[i, j] != 1)  
  36.                         {  
  37.                             continue;  
  38.                         }  
  39.   
  40.                         //取3*3領域  
  41.                         for (int m = 0; m < 3; m++)  
  42.                         {  
  43.                             for (int n = 0; n < 3; n++)  
  44.                             {  
  45.                                 nb[m, n] = input[i - 1 + m, j - 1 + n];  
  46.                             }  
  47.                         }  
  48.                         //復制  
  49.                         nnb[0] = nb[2, 1]==1?0:1;  
  50.                         nnb[1] = nb[2, 0]==1?0:1;  
  51.                         nnb[2] = nb[1, 0]==1?0:1;  
  52.                         nnb[3] = nb[0, 0]==1?0:1;  
  53.                         nnb[4] = nb[0, 1]==1?0:1;  
  54.                         nnb[5] = nb[0, 2]==1?0:1;  
  55.                         nnb[6] = nb[1, 2]==1?0:1;  
  56.                         nnb[7] = nb[2, 2]==1?0:1;  
  57.   
  58.                         // 條件2:p0,p2,p4,p6 不皆為前景點   
  59.                         if (nnb[0] == 0 && nnb[2] == 0 && nnb[4] == 0 && nnb[6] == 0)  
  60.                         {  
  61.                             continue;  
  62.                         }  
  63.                         // 條件3: p0~p7至少兩個是前景點   
  64.                         int iCount = 0;  
  65.                         for (int ii = 0; ii < 8; ii++)  
  66.                         {  
  67.                             iCount += nnb[ii];  
  68.                         }  
  69.                         if (iCount > 6) continue;   
  70.                           
  71.                         // 條件4:聯結數等於1   
  72.                         if (DetectConnectivity(nnb) != 1)  
  73.                         {  
  74.                             continue;  
  75.                         }  
  76.                         // 條件5: 假設p2已標記刪除,則令p2為背景,不改變p的聯結數   
  77.                         if (input[i, j - 1] == -1)  
  78.                         {  
  79.                             nnb[2] = 1;  
  80.                             if (DetectConnectivity(nnb) != 1)  
  81.                                 continue;  
  82.                             nnb[2] = 0;  
  83.                         }  
  84.                         // 條件6: 假設p4已標記刪除,則令p4為背景,不改變p的聯結數   
  85.                         if (input[i, j + 1] == -1)  
  86.                         {  
  87.                             nnb[6] = 1;  
  88.                             if (DetectConnectivity(nnb) != 1)  
  89.                                 continue;  
  90.                             nnb[6] = 0;  
  91.                         }  
  92.   
  93.                         input[i, j] = -1;  
  94.                         IsModified = true;  
  95.                     }  
  96.                 }  
  97.                 for (int i = 0; i < lWidth; i++)  
  98.                 {  
  99.                     for (int j = 0; j < lHeight; j++)  
  100.                     {  
  101.                         if (input[i, j] == -1)  
  102.                         {  
  103.                             input[i, j] = 0;  
  104.                         }  
  105.                     }  
  106.                 }  
  107.   
  108.             } while (IsModified);  
  109.   
  110.             return input;  
  111.         }  
arrow
arrow
    全站熱搜

    Rocky 發表在 痞客邦 留言(0) 人氣()