大津算法

在電腦視覺和圖像處理中，大津二值化法用來自動對基於聚類的圖像進行二值化， 或者說，將一個灰度圖像退化為二值圖像。該演算法以大津展之命名。演算法假定該圖像根據雙模直方圖（前景像素和背景像素）把包含兩類像素，於是它要計算能將兩類分開的最佳閾值，使得它們的類內變異數最小；由於兩兩平方距離恆定，所以即它們的類間變異數最大。 因此，大津二值化法粗略的來說就是一維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）可以很容易地轉變成多執行緒的方法。