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!