otsu算法原理及C++代碼實現-尋找灰度圖片中最佳閾值

           otsu法(最大類間方差法,有時也稱之爲大津算法)使用的是聚類的思想,把圖像的灰度數按灰度級分成2個部分,使得兩個部分之間的灰度值差異最大,每個部分之間的灰度差異最小,通過方差的計算來尋找一個合適的灰度級別 來劃分。  所以 可以在二值化的時候 採用otsu算法來自動選取閾值進行二值化。otsu算法被認爲是圖像分割中閾值選取的最佳算法,計算簡單,不受圖像亮度和對比度的影響。因此,使類間方差最大的分割意味着錯分概率最小。


設t爲設定的閾值。

wo: 分開後  前景像素點數佔圖像的比例

uo:  分開後  前景像素點的平均灰度

w1:分開後  被景像素點數佔圖像的比例

u1:  分開後  被景像素點的平均灰度

u=w0*u0 + w1*u1 :圖像總平均灰度


從L個灰度級遍歷t,使得t爲某個值的時候,前景和背景的方差最大, 則 這個 t  值便是我們要求得的閾值。

其中,方差的計算公式如下:

g=wo * (uo - u) * (uo - u) + w1 * (u1 - u) * (u1 - u)

[             此公式計算量較大,可以採用:      g = wo * w1 * (uo - u1) * (uo - u1)                ]




由於otsu算法是對圖像的灰度級進行聚類,so  在執行otsu算法之前,需要計算該圖像的灰度直方圖。


按照上面的解釋參考代碼如下:

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "cv.h"
  4. #include "highgui.h"
  5. #include "Math.h"
  6. int Otsu(IplImage* src);
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. IplImage* img = cvLoadImage("c:\\aSa.jpg",0);
  10. IplImage* dst = cvCreateImage(cvGetSize(img), 8, 1);
  11. int threshold = Otsu(img);
  12. cvThreshold(img, dst, threshold, 255, CV_THRESH_BINARY);
  13. cvNamedWindow( "img", 1 );
  14. cvShowImage("img", dst);
  15. cvWaitKey(-1);
  16. cvReleaseImage(&img);
  17. cvReleaseImage(&dst);
  18. cvDestroyWindow( "dst" );
  19. return 0;
  20. }
  21. int Otsu(IplImage* src)
  22. {
  23. int height=src->height;
  24. int width=src->width;
  25. long size = height * width;
  26. //histogram
  27. float histogram[256] = {0};
  28. for(int m=0; m < height; m++)
  29. {
  30. unsigned char* p=(unsigned char*)src->imageData + src->widthStep * m;
  31. for(int n = 0; n < width; n++)
  32. {
  33. histogram[int(*p++)]++;
  34. }
  35. }
  36. int threshold;
  37. long sum0 = 0, sum1 = 0; //存儲前景的灰度總和和背景灰度總和
  38. long cnt0 = 0, cnt1 = 0; //前景的總個數和背景的總個數
  39. double w0 = 0, w1 = 0; //前景和背景所佔整幅圖像的比例
  40. double u0 = 0, u1 = 0; //前景和背景的平均灰度
  41. double variance = 0; //最大類間方差
  42. int i, j;
  43. double u = 0;
  44. double maxVariance = 0;
  45. for(i = 1; i < 256; i++) //一次遍歷每個像素
  46. {
  47. sum0 = 0;
  48. sum1 = 0;
  49. cnt0 = 0;
  50. cnt1 = 0;
  51. w0 = 0;
  52. w1 = 0;
  53. for(j = 0; j < i; j++)
  54. {
  55. cnt0 += histogram[j];
  56. sum0 += j * histogram[j];
  57. }
  58. u0 = (double)sum0 / cnt0;
  59. w0 = (double)cnt0 / size;
  60. for(j = i ; j <= 255; j++)
  61. {
  62. cnt1 += histogram[j];
  63. sum1 += j * histogram[j];
  64. }
  65. u1 = (double)sum1 / cnt1;
  66. w1 = 1 - w0; // (double)cnt1 / size;
  67. u = u0 * w0 + u1 * w1; //圖像的平均灰度
  68. printf("u = %f\n", u);
  69. //variance = w0 * pow((u0 - u), 2) + w1 * pow((u1 - u), 2);
  70. variance = w0 * w1 * (u0 - u1) * (u0 - u1);
  71. if(variance > maxVariance)
  72. {
  73. maxVariance = variance;
  74. threshold = i;
  75. }
  76. }
  77. printf("threshold = %d\n", threshold);
  78. return threshold;
  79. }



快哭了 把w1寫成w0 ··害我debug 了好久~~總是不認真,腦袋渾渾噩噩的···這都看不出來。。。。委屈


==================

對了,之前蒐集的一個otsu的算法,代碼如下(由於時間太久了,不知道出處了。。。膜拜大牛哈偷笑

  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "cv.h"
  4. #include "highgui.h"
  5. #include "Math.h"
  6. int Otsu(IplImage* src);
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9. IplImage* img = cvLoadImage("c:\\aSa.jpg",0);
  10. IplImage* dst = cvCreateImage(cvGetSize(img), 8, 1);
  11. int threshold = Otsu(img);
  12. printf("threshold = %d\n", threshold);
  13. cvThreshold(img, dst, threshold, 255, CV_THRESH_BINARY);
  14. cvNamedWindow( "img", 1 );
  15. cvShowImage("img", dst);
  16. cvWaitKey(-1);
  17. cvReleaseImage(&img);
  18. cvReleaseImage(&dst);
  19. cvDestroyWindow( "dst" );
  20. return 0;
  21. }
  22. int Otsu(IplImage* src)
  23. {
  24. int height=src->height;
  25. int width=src->width;
  26. //histogram
  27. float histogram[256] = {0};
  28. for(int i=0; i < height; i++)
  29. {
  30. unsigned char* p=(unsigned char*)src->imageData + src->widthStep * i;
  31. for(int j = 0; j < width; j++)
  32. {
  33. histogram[*p++]++;
  34. }
  35. }
  36. //normalize histogram
  37. int size = height * width;
  38. for(int i = 0; i < 256; i++)
  39. {
  40. histogram[i] = histogram[i] / size;
  41. }
  42. //average pixel value
  43. float avgValue=0;
  44. for(int i=0; i < 256; i++)
  45. {
  46. avgValue += i * histogram[i]; //整幅圖像的平均灰度
  47. }
  48. int threshold;
  49. float maxVariance=0;
  50. float w = 0, u = 0;
  51. for(int i = 0; i < 256; i++)
  52. {
  53. w += histogram[i]; //假設當前灰度i爲閾值, 0~i 灰度的像素(假設像素值在此範圍的像素叫做前景像素) 所佔整幅圖像的比例
  54. u += i * histogram[i]; // 灰度i 之前的像素(0~i)的平均灰度值: 前景像素的平均灰度值
  55. float t = avgValue * w - u;
  56. float variance = t * t / (w * (1 - w) );
  57. if(variance > maxVariance)
  58. {
  59. maxVariance = variance;
  60. threshold = i;
  61. }
  62. }
  63. return threshold;
  64. }



轉自

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章