大津法 OTSU的算法

大津法由大津於1979年提出,對圖像Image,記t爲前景與背景的分割閾值,前景點數佔圖像比例爲w0,平均灰度爲u0;背景點數佔圖像比例爲w1,平均灰度爲u1。圖像的總平均灰度爲:u=w0*u0+w1*u1。從最小灰度值到最大灰度值遍歷t,當t使得值g=w0*(u0-u)2+w1*(u1-u)2 最大時t即爲分割的最佳閾值。對大津法可作如下理解:該式實際上就是類間方差值,閾值t分割出的前景和背景兩部分構成了整幅圖像,而前景取值u0,概率爲 w0,背景取值u1,概率爲w1,總均值爲u,根據方差的定義即得該式。因方差是灰度分佈均勻性的一種度量,方差值越大,說明構成圖像的兩部分差別越大, 當部分目標錯分爲背景或部分背景錯分爲目標都會導致兩部分差別變小,因此使類間方差最大的分割意味着錯分概率最小。

直接應用大津法計算量較大,因此我們在實現時採用了等價的公式g=w0*w1*(u0-u1)2。部分計算過程如下:

  1. //遍歷所有灰度值求Max g。 
  2. for intCurrentLevel:=0 to intArrLen do 
  3.   begin 
  4.     if intSclGrayLevel[intCurrentLevel]=0 then 
  5.       continue 
  6.     else 
  7.       begin 
  8.               // 計算當閾值爲intCurrentLevel時的g 
  9.         intCount:=0; 
  10.         intSumPels:=0; 
  11.  
  12.         for intLoop:=0 to intCurrentLevel do 
  13.           begin 
  14.             intCount:=intCount+intSclGrayLevel[intLoop]; 
  15.             intSumPels:=intSumPels+intSumPelsArr[intLoop]; 
  16.           end; 
  17.         w0:=intCount/intSize; 
  18.         u0:=intSumPels/intCount; 
  19.         w1:=1-w0; 
  20.         if intSize-intCount<>0 then 
  21.           u1:=(intTotalPels-intSumPels)/(intSize-intCount) 
  22.         else 
  23.           u1:=0; 
  24.  
  25.         RlTempO:=w0*w1*(u0-u1)*(u0-u1); 
  26.         if RlTempO>RlMaxO then 
  27.         begin 
  28.           RlMaxO:=RlTempO; 
  29.           Result:=intCurrentLevel; 
  30.         end; 
  31.       end; 


我們在測試中發現:大津法選取出來的閾值非常理想,對各種情況的表現都較爲良好。雖然它在很多情況下都不是最佳的分割,但分割質量通常都有一定的保障,可以說是最穩定的分割。由上可知,大津算法是一種較爲通用的分割算法。在它的思想的啓迪下,人們進一步提出了多種類似的評估閾值的算法,具體可參加【5】、【6】等。

 

 

 

 OTSU的算法,很好用,好不容易纔找到的。

  1. /* 
  2. OTSU 算法可以說是自適應計算單閾值(用來轉換灰度圖像爲二值圖像)的簡單高效方法。 
  3.  
  4. 下面的代碼最早由 Ryan Dibble提供,此後經過多人Joerg.Schulenburg, R.Z.Liu 等修改,補正。 
  5.  
  6. 算法對輸入的灰度圖像的直方圖進行分析,將直方圖分成兩個部分,使得兩部分之間的距離最大。 
  7. 劃分點就是求得的閾值。 
  8.  
  9. parameter: *image --- buffer for image 
  10. rows, cols --- size of image 
  11. x0, y0, dx, dy --- region of vector used for computing threshold 
  12. vvv --- debug option, is 0, no debug information outputed 
  13. */ 
  14. /*======================================================================*/ 
  15. /* OTSU global thresholding routine */ 
  16. /* takes a 2D unsigned char array pointer, number of rows, and */ 
  17. /* number of cols in the array. returns the value of the threshold */ 
  18. /*======================================================================*/ 
  19. int otsu (unsigned char *image, int rows, int cols, int x0, int y0, int dx, int dy, int vvv) 
  20. unsigned char *np; // 圖像指針 
  21. int thresholdValue=1; // 閾值 
  22. int ihist[256]; // 圖像直方圖,256個點 
  23.  
  24. int i, j, k; // various counters 
  25. int n, n1, n2, gmin, gmax; 
  26. double m1, m2, sum, csum, fmax, sb; 
  27.  
  28. // 對直方圖置零... 
  29. memset(ihist, 0, sizeof(ihist)); 
  30.  
  31. gmin=255; gmax=0; 
  32. // 生成直方圖 
  33. for (i = y0 + 1; i < y0 + dy - 1; i++) { 
  34. np = [i*cols+x0+1]; 
  35. for (j = x0 + 1; j < x0 + dx - 1; j++) { 
  36. ihist[*np]++; 
  37. if(*np > gmax) gmax=*np; 
  38. if(*np < gmin) gmin=*np; 
  39. np++; /* next pixel */ 
  40.  
  41. // set up everything 
  42. sum = csum = 0.0; 
  43. n = 0; 
  44.  
  45. for (k = 0; k <= 255; k++) { 
  46. sum += (double) k * (double) ihist[k]; /* x*f(x) 質量矩*/ 
  47. n += ihist[k]; /* f(x) 質量 */ 
  48.  
  49. if (!n) { 
  50. // if n has no value, there is problems... 
  51. fprintf (stderr, "NOT NORMAL thresholdValue = 160/n"); 
  52. return (160); 
  53.  
  54. // do the otsu global thresholding method 
  55. fmax = -1.0; 
  56. n1 = 0; 
  57. for (k = 0; k < 255; k++) { 
  58. n1 += ihist[k]; 
  59. if (!n1) { continue; } 
  60. n2 = n - n1; 
  61. if (n2 == 0) { break; } 
  62. csum += (double) k *ihist[k]; 
  63. m1 = csum / n1; 
  64. m2 = (sum - csum) / n2; 
  65. sb = (double) n1 *(double) n2 *(m1 - m2) * (m1 - m2); 
  66. /* bbg: note: can be optimized. */ 
  67. if (sb > fmax) { 
  68. fmax = sb; 
  69. thresholdValue = k; 
  70.  
  71. // at this point we have our thresholding value 
  72.  
  73. // debug code to display thresholding values 
  74. if ( vvv & 1 ) 
  75. fprintf(stderr,"# OTSU: thresholdValue = %d gmin=%d gmax=%d/n"
  76. thresholdValue, gmin, gmax); 
  77.  
  78. return(thresholdValue); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章