大津算法

在计算机视觉和图像处理中，大津二值化法用来自动对基于聚类的图像进行二值化， 或者说，将一个灰度图像退化为二值图像。该算法以大津展之命名。算法假定该图像根据双模直方图（前景像素和背景像素）把包含两类像素，于是它要计算能将两类分开的最佳阈值，使得它们的类内方差最小；由于两两平方距离恒定，所以即它们的类间方差最大。 因此，大津二值化法粗略的来说就是一维Fisher判别分析的离散化模拟。
原始方法的多级阈值扩展称为多大津算法。

JavaScript实现

注：输入参数total是给定图像中的像素数。输入参数histogram是灰度图像不同灰度级（典型的8位图像）的256元直方图。此函数输出图像的阈值。
function otsu(histogram, total) {
    var sum = 0;
    for (var i = 1; i < 256; ++i)
        sum += i * histogram[i];
    var sumB = 0;
    var wB = 0;
    var wF = 0;
    var mB;
    var mF;
    var max = 0.0;
    var between = 0.0;
    var threshold1 = 0.0;
    var threshold2 = 0.0;
    for (var i = 0; i < 256; ++i) {
        wB += histogram[i];
        if (wB == 0)
            continue;
        wF = total - wB;
        if (wF == 0)
            break;
        sumB += i * histogram[i];
        mB = sumB / wB;
        mF = (sum - sumB) / wF;
        between = wB * wF * (mB - mF) * (mB - mF);
        if ( between >= max ) {
            threshold1 = i;
            if ( between > max ) {
                threshold2 = i;
            }
            max = between;            
        }
    }
    return ( threshold1 + threshold2 ) / 2.0;}

C实现

1 unsigned char otsu_threshold( int* histogram, int pixel_total ) 2 { 3     unsigned int sumB = 0; 4     unsigned int sum1 = 0; 5     float wB = 0.0f; 6     float wF = 0.0f; 7     float mF = 0.0f; 8     float max_var = 0.0f; 9     float inter_var = 0.0f;10     unsigned char threshold = 0;11     unsigned short index_histo = 0;12 13     for ( index_histo = 1; index_histo < 256; ++index_histo )14     {15         sum1 += index_histo * histogram[ index_histo ];16     }17 18     for (index_histo = 1; index_histo < 256; ++index_histo)19     {20         wB = wB + histogram[ index_histo ];21         wF = pixel_total - wB;22         if ( wB == 0 || wF == 0 )23         {24             continue;25         }26         sumB = sumB + index_histo * histogram[ index_histo ];27         mF = ( sum1 - sumB ) / wF;28         inter_var = wB * wF * ( ( sumB / wB ) - mF ) * ( ( sumB / wB ) - mF );29         if ( inter_var >= max_var )30         {31             threshold = index_histo;32             max_var = inter_var;33         }34     }35 36     return threshold;37 }

MATLAB实现

total为给定图像中的像素数。 histogramCounts为灰度图像不同灰度级（典型的8位图像）的256元直方图。 level为图像的阈值（double）。
function level = otsu(histogramCounts, total)%% OTSU automatic thresholding methodsumB = 0;wB = 0;maximum = 0.0;threshold1 = 0.0;threshold2 = 0.0;sum1 = sum((1:256).*histogramCounts.'); % the above code is replace with this single linefor ii=1:256
    wB = wB + histogramCounts(ii);
    if (wB == 0)
        continue;
    end
    wF = total - wB;
    if (wF == 0)
        break;
    end
    sumB = sumB +  ii * histogramCounts(ii);
    mB = sumB / wB;
    mF = (sum1 - sumB) / wF;
    between = wB * wF * (mB - mF) * (mB - mF);
    if ( between >= maximum )
        threshold1 = ii;
        if ( between > maximum )
            threshold2 = ii;
        end
        maximum = between;
    endendlevel = (threshold1 + threshold2 )/(2);end
另一种方法是用向量化方法（可以很容易地转换为便于GPU处理Python矩阵数组版本）
function  [threshold_otsu] = Thredsholding_Otsu( Image)%Intuition:%(1)pixels are divided into two groups%(2)pixels within each group are very similar to each other %   Parameters:%   t : threshold %   r : pixel value ranging from 1 to 255%   q_L, q_H : the number of lower and higher group respectively%   sigma : group variance%   miu : group mean%   Author: Lei Wang%   Date  : 22/09/2013%   References : Wikepedia, %   for multi children Otsu method, please visit : https://drive.google.com/file/d/0BxbR2jt9XyxteF9fZ0NDQ0dKQkU/view?usp=sharing%   This is my original worknbins = 256;counts = imhist(Image,nbins);p = counts / sum(counts);for t = 1 : nbins
   q_L = sum(p(1 : t));
   q_H = sum(p(t + 1 : end));
   miu_L = sum(p(1 : t) .* (1 : t)') / q_L;
   miu_H = sum(p(t + 1 : end) .* (t + 1 : nbins)') / q_H;
   sigma_b(t) = q_L * q_H * (miu_L - miu_H)^2;end[~,threshold_otsu] = max(sigma_b(:));end
该实现有一点点冗余的计算。但由于大津算法快速，这个实现版本是可以接受，并且易于理解的。因为在一些环境中，如果使用向量化的形式，可以更快地运算循环。大津二值化法使用架构最小的堆－孩子标签（heap-children label）可以很容易地转变成多线程的方法。