Otsu Thresholding

1. Otsu Thresholding Explained

Otsu對image中的所有像素都假定爲閾值,然後根據此值將image分爲前景物體和背景;遍歷所有像素值
計算類內方差,最小的類內方差對應的threshold即爲最優閾值;

以6階灰度圖像爲例


A 6-level greyscale image and its histogram

計算閾值爲3的情況,即T=3,分別計算前景物體和背景的方差;



計算類內方差Within-Class Variance


下圖列出了所有的可能閾值的計算結果


可以觀察到T=3對應的類內方差最小,所以最優閾值爲T=3;


2.A Faster Approach

若直接使用上一節所述的計算方式,計算量比較大,可以採用計算類間方差between class variance,的方式


計算結果如下,最小的類內方差對應最大的類間方差,而類間方差僅需要權重和均值;


3.Java Implementation

下述代碼爲java實現,可以方便的改爲c/c++等語言;

// Calculate histogram
int ptr = 0;
while (ptr < srcData.length) {
   int h = 0xFF & srcData[ptr];
   histData[h] ++;
   ptr ++;
}

// Total number of pixels
int total = srcData.length;

float sum = 0;
for (int t=0 ; t<256 ; t++) sum += t * histData[t];

float sumB = 0;
int wB = 0;
int wF = 0;

float varMax = 0;
threshold = 0;

for (int t=0 ; t<256 ; t++) {
   wB += histData[t];               // Weight Background
   if (wB == 0) continue;

   wF = total - wB;                 // Weight Foreground
   if (wF == 0) break;

   sumB += (float) (t * histData[t]);

   float mB = sumB / wB;            // Mean Background
   float mF = (sum - sumB) / wF;    // Mean Foreground

   // Calculate Between Class Variance
   float varBetween = (float)wB * (float)wF * (mB - mF) * (mB - mF);

   // Check if new maximum found
   if (varBetween > varMax) {
      varMax = varBetween;
      threshold = t;
   }
}
4.Example

譯文地址

over!

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