大津の二値化とは? わかりやすく解説

大津の二値化法

(大津の二値化 から転送)

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2022/05/13 17:53 UTC 版)

ナビゲーションに移動 検索に移動
大津の二値化でしきい値処理した画像の一例
元の画像

大津の二値化法(おおつのにちかほう、Otsu's method、大津の方法などとも呼ばれる)とは、コンピュータビジョン画像処理において、自動画像しきい値処理を行うために使用される手法[1]。その名は大津展之英語版にちなむ。最も単純な形式では、アルゴリズムはピクセルをforegroundとbackgroundの2つのクラスに分ける1つの強度しきい値を返す。このしきい値はクラス内の強度分散を最小化することにより、または同等にクラス間の分散を最大化することにより決定される[2]

大津の二値化はフィッシャーの判別分析の1次元の離散に相当するものであり、ジェンクス最適化法英語版に関連しており、強度ヒストグラムで行われる大域最適なK平均法と同等である[3]。マルチレベルのしきい値処理へ拡大することは最初の論文で説明されており[2]、以降計算効率の高い実装が提案されている[4][5]

大津の二値化

大津の二値化を視覚化したもの

アルゴリズムは、2つのクラスの分散の加重和として定義されるクラス内分散を最小化するしきい値を探す。

加重はしきい値により分けられた2つのクラスの確率であり、はこれら2つのクラスの分散である。

クラスの確率はヒストグラムの個のビンから計算される。

2つのクラスでは、クラス内分散を最小化することはクラス間分散を最大化することと同じである[2]

これはクラス確率とクラス平均で表され、クラス平均およびは、

である。以上の関係は以下のように簡単に確かめることができる。

クラス確率とクラス平均は、繰り返し計算することができる。このアイデアは効果的なアルゴリズムを生む。

アルゴリズム

  1. 各強度レベルのヒストグラムと確率を計算する。
  2. の初期値を設定する。
  3. 最大強度までの全ての考えうるしきい値で次のステップを行う。
    1. を更新する。
    2. を計算する。
  4. 望ましいしきい値はが最大となる値である。

MATLABまたはOctaveでの実装

histogramCountsはさまざまなグレーレベルのグレースケール画像(通常は8ビット画像)の256画素のヒストグラムである。levelは画像のしきい値 (double) である。


function level = otsu(histogramCounts)
total = sum(histogramCounts); % total number of pixels in the image 
%% OTSU automatic thresholding
top = 256;
sumB = 0;
wB = 0;
maximum = 0.0;
sum1 = dot(0:top-1, histogramCounts);
for ii = 1:top
    wF = total - wB;
    if wB > 0 && wF > 0
        mF = (sum1 - sumB) / wF;
        val = wB * wF * ((sumB / wB) - mF) * ((sumB / wB) - mF);
        if ( val >= maximum )
            level = ii;
            maximum = val;
        end
    end
    wB = wB + histogramCounts(ii);
    sumB = sumB + (ii-1) * histogramCounts(ii);
end
end

MatlabにはImage Processing Toolboxに組み込み関数graythresh()multithresh()があり、それぞれ大津の手法とマルチ大津の手法(Multi Otsu's method)で実装されている。

Pythonでの実装

この実装にはNumPyライブラリが必要である

def compute_otsu_criteria(im, th):
    # create the thresholded image
    thresholded_im = np.zeros(im.shape)
    thresholded_im[im >= th] = 1

    # compute weights
    nb_pixels = im.size
    nb_pixels1 = np.count_nonzero(th)
    weight1 = nb_pixels1 / nb_pixels
    weight0 = 1 - weight1

    # if one the classes is empty, eg all pixels are below or above the threshold, that threshold will not be considered
    # in the search for the best threshold
    if weight1 == 0 or weight0 == 0:
        return np.nan

    # find all pixels belonging to each class
    val_pixels1 = im[thresholded_im == 1]
    val_pixels0 = im[thresholded_im == 0]

    # compute variance of these classes
    var0 = np.var(val_pixels0) if len(val_pixels0) > 0 else 0
    var1 = np.var(val_pixels1) if len(val_pixels1) > 0 else 0

    return weight0 * var0 + weight1 * var1

im = # load your image as a numpy array.
# For testing purposes, one can use for example im = np.random.randint(0,255, size = (50,50))

# testing all thresholds from 0 to the maximum of the image
threshold_range = range(np.max(im)+1)
criterias = [compute_otsu_criteria(im, th) for th in threshold_range]

# best threshold is the one minimizing the Otsu criteria
best_threshold = threshold_range[np.argmin(criterias)]

OpenCVやScikit-imageなどの画像処理専用のPythonライブラリは、アルゴリズムの組み込み実装を提案する。

限界とバリエーション

大津の方法は、ヒストグラムが2つのピークの間に深く鋭い谷を持つ二峰性分布を有するときにうまく機能する[6]

他の全ての大域的なしきい値処理と同様に、ノイズが大きく、対象のサイズが小さく、明暗が不均一で、クラス内の分散がクラス間の分散よりも大きい場合にパフォーマンスが低下する[7]。このような場合、大津の方法を局所的に適用することが開発されている[8]

さらに、大津の方法の数学的根拠は、画像のヒストグラムを分散が等しくサイズが等しい2つの正規分布の混合としてモデル化する[9]。しかし、大津のしきい値処理はこれらの仮定を満たさない場合でも満足のいく結果をもたらす可能性がある。同様に統計検定(大津の方法が密接に関連する[10])は、動作の仮定が完全に満たされていない場合でも正しく実行される。しかし、これらの仮定から生じる深刻な逸脱を無くすために、Kittler-Illingworthの方法などの大津の方法のいくつかのバリエーションが提案されている[9][11]

ノイズの多い画像に対するバリエーション

大津の方法の限界に対処するためにさまざまな拡張が開発されてきた。人気のある拡張の1つは、ノイズの多い画像のオブジェクトセグメンテーションのタスクに適した2次元大津の方法である。ここでは、特定のピクセルの強度値をそのすぐ近傍の平均強度と比較することでセグメンテーション結果を改良する[8]

各ピクセルで、近傍の平均グレーレベル値が計算される。与えられたピクセルのグレーレベルを個の離散値に分割し、平均グレーレベルも同じ個の値に分割する。その後、ピクセルのグレーレベルと近傍の平均のペアが形成される。各ペアは個の考えうる2次元ビンの1つに属する。ペアの出現の総数(頻度)を画像のピクセルの総数で割ったものは、2次元ヒストグラムで同時確率密度関数を定義する。

そして、2次元大津の方法は、2次元ヒストグラムに基づいて次のように開発されている。

2つのクラスの確率は、次のように表せる。

2つのクラスの強度平均ベクトルと合計平均ベクトルは次のように表せる。

ほとんどの場合、非対角の確率はわずかであるため、次のことを簡単に確認することができる。

クラス間離散行列は次のように定義される。

離散行列のトレースは次のように表される。

ここで

1次元の大津の方法と同様に、最適なしきい値は、を最大化することで取得される。

アルゴリズム

は、1次元の大津の方法と同様に繰り返し取得される。の値は、の最大値を取得するまで変更される。つまり、

max,s,t = 0;
for ss: 0 to L-1 do
    for tt: 0 to L-1 do
        evaluate tr(S_b);
        if tr(S_b) > max
            max = tr(S,b);
            s = ss;
            t = tt;
        end if
    end for
end for
return s,t;

を評価するために、高速再起動的プログラミングアルゴリズムを使用して時間パフォーマンスを向上させることができることに留意してください[12]。しかし、動的プログラミングのアプローチを使用しても、2次元大津の方法は依然として時間計算量が非常に複雑である。したがって、計算コストを削減するために多くの研究が行われてきた[13]

合計領域テーブルを使用して3つのテーブルを作製する場合は、を合計し、を合計し、を合計し、その時のランタイムの複雑性は(O(N_pixels), O(N_bins*N_bins))の最大値である。しきい値に関して粗い解像度のみが必要な場合はN_binsを減らすことができることに注意してください。

en:Summed-area table参照

Matlabでの実装

関数の入力と出力

histsは、グレースケール値と近傍平均グレースケール値のペアのの2次元ヒストグラムである。

totalは、所与の画像のペアの数である。これは各方向の2次元ヒストグラムのビンの数により決まる。

thresholdは取得されたしきい値である。

function threshold = otsu_2D(hists, total)
maximum = 0.0;
threshold = 0;
helperVec = 0:255;
mu_t0 = sum(sum(repmat(helperVec',1,256).*hists));
mu_t1 = sum(sum(repmat(helperVec,256,1).*hists));
p_0 = zeros(256);
mu_i = p_0;
mu_j = p_0;
for ii = 1:256
    for jj = 1:256
        if jj == 1
            if ii == 1
                p_0(1,1) = hists(1,1);
            else
                p_0(ii,1) = p_0(ii-1,1) + hists(ii,1);
                mu_i(ii,1) = mu_i(ii-1,1)+(ii-1)*hists(ii,1);
                mu_j(ii,1) = mu_j(ii-1,1);
            end
        else
            p_0(ii,jj) = p_0(ii,jj-1)+p_0(ii-1,jj)-p_0(ii-1,jj-1)+hists(ii,jj); % THERE IS A BUG HERE. INDICES IN MATLAB MUST BE HIGHER THAN 0. ii-1 is not valid
            mu_i(ii,jj) = mu_i(ii,jj-1)+mu_i(ii-1,jj)-mu_i(ii-1,jj-1)+(ii-1)*hists(ii,jj);
            mu_j(ii,jj) = mu_j(ii,jj-1)+mu_j(ii-1,jj)-mu_j(ii-1,jj-1)+(jj-1)*hists(ii,jj);
        end

        if (p_0(ii,jj) == 0)
            continue;
        end
        if (p_0(ii,jj) == total)
            break;
        end
        tr = ((mu_i(ii,jj)-p_0(ii,jj)*mu_t0)^2 + (mu_j(ii,jj)-p_0(ii,jj)*mu_t1)^2)/(p_0(ii,jj)*(1-p_0(ii,jj)));

        if ( tr >= maximum )
            threshold = ii;
            maximum = tr;
        end
    end
end
end

不平衡な画像に対するバリエーション

画像のクラスのグレーレベルは正規分布とみなすことができるが、サイズや分散が等しくない場合、大津のアルゴリズムの仮定は満たされない。Kittler-Illingworthのアルゴリズム(最小エラーしきい値処理とも呼ばれる)[11]はこのような場合を処理するための大津の方法のバリエーションである。このアルゴリズムを数学的に説明する方法はいくつかある。それらの1つは、試験される各しきい値について結果の2値画像の正規分布のパラメータがデータが与えられた最尤推定により推定されたことを考慮することである[9]

このアルゴリズムは大津の方法よりも優れているように見えることがあるが、推定される新しいパラメータが導入されるためアルゴリズムが過剰パラメータ化されて不安定になる可能性がある。大津の方法からの仮定が少なくとも部分的に有効であると考えられる多くの場合において、オッカムの剃刀に従ってKittler-Illingworthのアルゴリズムよりも大津の方法を優先する方が好ましい場合がある[9]

出典

  1. ^ M. Sezgin & B. Sankur (2004). “Survey over image thresholding techniques and quantitative performance evaluation”. Journal of Electronic Imaging 13 (1): 146-165. doi:10.1117/1.1631315. 
  2. ^ a b c Nobuyuki Otsu (1979). “A threshold selection method from gray-level histograms”. IEEE Trans. Sys. Man. Cyber. 9 (1): 62-66. doi:10.1109/TSMC.1979.4310076. 
  3. ^ Liu, Dongju (2009). “Otsu method and K-means”. Ninth International Conference on Hybrid Intelligent Systems IEEE 1: 344-349. 
  4. ^ Liao, Ping-Sung (2001). “A fast algorithm for multilevel thresholding”. J. Inf. Sci. Eng. 17 (5): 713-727. オリジナルの2019-06-24時点におけるアーカイブ。. https://web.archive.org/web/20190624180800/https://pdfs.semanticscholar.org/b809/14dbbee9f6b2455742d8117417731e6ecf12.pdf. 
  5. ^ Huang, Deng-Yuan (2009). “Optimal multi-level thresholding using a two-stage Otsu optimization approach”. Pattern Recognition Letters 30 (3): 275-284. doi:10.1016/j.patrec.2008.10.003. 
  6. ^ Kittler, J.; Illingworth, J. (September 1985). “On threshold selection using clustering criteria”. IEEE Transactions on Systems, Man, and Cybernetics SMC-15 (5): 652-655. doi:10.1109/tsmc.1985.6313443. ISSN 0018-9472. https://doi.org/10.1109/tsmc.1985.6313443. 
  7. ^ Lee, Sang Uk and Chung, Seok Yoon and Park, Rae Hong (1990). “A comparative performance study of several global thresholding techniques for segmentation”. Computer Vision, Graphics, and Image Processing 52 (2): 171-190. doi:10.1016/0734-189x(90)90053-x. 
  8. ^ a b Jianzhuang, Liu and Wenqing, Li and Yupeng, Tian (1991). “Automatic thresholding of gray-level pictures using two-dimension Otsu method”. Circuits and Systems, 1991. Conference Proceedings, China., 1991 International Conference on: 325-327. 
  9. ^ a b c d Kurita, T.; Otsu, N.; Abdelmalek, N. (October 1992). “Maximum likelihood thresholding based on population mixture models”. Pattern Recognition 25 (10): 1231-1240. doi:10.1016/0031-3203(92)90024-d. ISSN 0031-3203. https://doi.org/10.1016/0031-3203(92)90024-d. 
  10. ^ Jing-Hao Xue; Titterington, D. M. (August 2011). “$t$-Tests, $F$-Tests and Otsu's Methods for Image Thresholding”. IEEE Transactions on Image Processing 20 (8): 2392-2396. doi:10.1109/tip.2011.2114358. ISSN 1057-7149. https://doi.org/10.1109/tip.2011.2114358. 
  11. ^ a b Kittler, J.; Illingworth, J. (1986-01-01). “Minimum error thresholding” (英語). Pattern Recognition 19 (1): 41-47. doi:10.1016/0031-3203(86)90030-0. ISSN 0031-3203. https://www.sciencedirect.com/science/article/pii/0031320386900300. 
  12. ^ Zhang, Jun & Hu, Jinglu (2008). “Image segmentation based on 2D Otsu method with histogram analysis”. Computer Science and Software Engineering, 2008 International Conference on 6: 105-108. doi:10.1109/CSSE.2008.206. ISBN 978-0-7695-3336-0. 
  13. ^ Zhu, Ningbo and Wang, Gang and Yang, Gaobo and Dai, Weiming (2009). “A fast 2d otsu thresholding algorithm based on improved histogram”. Pattern Recognition, 2009. CCPR 2009. Chinese Conference on: 1-5. 

外部リンク


大津の二値化

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2022/05/13 17:53 UTC 版)

大津の二値化法」の記事における「大津の二値化」の解説

アルゴリズムは、2つクラス分散加重和として定義されるクラス内分散を最小化するしきい値探す。 σ w 2 ( t ) = ω 0 ( t ) σ 0 2 ( t ) + ω 1 ( t ) σ 1 2 ( t ) {\displaystyle \sigma _{w}^{2}(t)=\omega _{0}(t)\sigma _{0}^{2}(t)+\omega _{1}(t)\sigma _{1}^{2}(t)} 加重 ω 0 {\displaystyle \omega _{0}} と ω 1 {\displaystyle \omega _{1}} はしきい値 t {\displaystyle t} により分けられた2つクラス確率であり、 σ 0 2 {\displaystyle \sigma _{0}^{2}} と σ 1 2 {\displaystyle \sigma _{1}^{2}} はこれら2つクラス分散である。 クラス確率 ω 0 , 1 ( t ) {\displaystyle \omega _{0,1}(t)} はヒストグラムの L {\displaystyle L} 個のビンから計算される。 ω 0 ( t ) = ∑ i = 0 t − 1 p ( i ) ω 1 ( t ) = ∑ i = t L1 p ( i ) {\displaystyle {\begin{aligned}\omega _{0}(t)&=\sum _{i=0}^{t-1}p(i)\\[4pt]\omega _{1}(t)&=\sum _{i=t}^{L-1}p(i)\end{aligned}}} 2つクラスでは、クラス内分散を最小化することはクラス間分散を最大化することと同じである。 σ b 2 ( t ) = σ 2 − σ w 2 ( t ) = ω 0 ( t ) ( μ 0 − μ T ) 2 + ω 1 ( t ) ( μ 1 − μ T ) 2 = ω 0 ( t ) ω 1 ( t ) [ μ 0 ( t ) − μ 1 ( t ) ] 2 {\displaystyle {\begin{aligned}\sigma _{b}^{2}(t)&=\sigma ^{2}-\sigma _{w}^{2}(t)=\omega _{0}(t)(\mu _{0}-\mu _{T})^{2}+\omega _{1}(t)(\mu _{1}-\mu _{T})^{2}\\&=\omega _{0}(t)\omega _{1}(t)\left[\mu _{0}(t)-\mu _{1}(t)\right]^{2}\end{aligned}}} これはクラス確率 ω {\displaystyle \omega } とクラス平均 μ {\displaystyle \mu } で表されクラス平均 μ 0 ( t ) {\displaystyle \mu _{0}(t)} 、 μ 1 ( t ) {\displaystyle \mu _{1}(t)} および μ T {\displaystyle \mu _{T}} は、 μ 0 ( t ) = ∑ i = 0 t − 1 i p ( i ) ω 0 ( t ) μ 1 ( t ) = ∑ i = t L − 1 i p ( i ) ω 1 ( t ) μ T = ∑ i = 0 L − 1 i p ( i ) {\displaystyle {\begin{aligned}\mu _{0}(t)&={\frac {\sum _{i=0}^{t-1}ip(i)}{\omega _{0}(t)}}\\[4pt]\mu _{1}(t)&={\frac {\sum _{i=t}^{L-1}ip(i)}{\omega _{1}(t)}}\\\mu _{T}&=\sum _{i=0}^{L-1}ip(i)\end{aligned}}} である。以上の関係は以下のように簡単に確かめることができる。 ω 0 μ 0 + ω 1 μ 1 = μ T ω 0 + ω 1 = 1 {\displaystyle {\begin{aligned}\omega _{0}\mu _{0}+\omega _{1}\mu _{1}&=\mu _{T}\\\omega _{0}+\omega _{1}&=1\end{aligned}}} クラス確率クラス平均は、繰り返し計算することができる。このアイデア効果的なアルゴリズム生む

※この「大津の二値化」の解説は、「大津の二値化法」の解説の一部です。
「大津の二値化」を含む「大津の二値化法」の記事については、「大津の二値化法」の概要を参照ください。

ウィキペディア小見出し辞書の「大津の二値化」の項目はプログラムで機械的に意味や本文を生成しているため、不適切な項目が含まれていることもあります。ご了承くださいませ。 お問い合わせ


英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「大津の二値化」の関連用語

大津の二値化のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



大津の二値化のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
ウィキペディアウィキペディア
All text is available under the terms of the GNU Free Documentation License.
この記事は、ウィキペディアの大津の二値化法 (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。 Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。
ウィキペディアウィキペディア
Text is available under GNU Free Documentation License (GFDL).
Weblio辞書に掲載されている「ウィキペディア小見出し辞書」の記事は、Wikipediaの大津の二値化法 (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。

©2025 GRAS Group, Inc.RSS