In image processing, the balanced histogram thresholding method (BHT),[1] is a very simple method used for automatic image thresholding. Like Otsu's Method[2] and the Iterative Selection Thresholding Method,[3] this is a histogram based thresholding method. This approach assumes that the image is divided in two main classes: The background and the foreground. The BHT method tries to find the optimum threshold level that divides the histogram in two classes.



This method weighs the histogram, checks which of the two sides is heavier, and removes weight from the heavier side until it becomes the lighter. It repeats the same operation until the edges of the weighing scale meet.
Given its simplicity, this method is a good choice as a first approach when presenting the subject of automatic image thresholding.
The following listing, in C notation, is a simplified version of the Balanced Histogram Thresholding method:
int BHThreshold ( int [] histogram ) { i_m = ( int )(( i_s + i_e ) / 2.0f ); // 秤の中心 I_m w_l = get_weight ( i_s , i_m + 1 , histogram ); // 左側の重量 W_l w_r = get_weight ( i_m + 1 , i_e + 1 , histogram ); // 右側の重量 W_r while ( i_s <= i_e ) { if ( w_r > w_l ) { // 右側が重いw_r -= histogram [ i_e -- ]; if ((( i_s + i_e ) / 2 ) < i_m ) { w_r += histogram [ i_m ]; w_l -= histogram [ i_m -- ]; } } else if ( w_l >= w_r ) { // 左側が重いw_l -= histogram [ i_s ++ ]; if ((( i_s + i_e ) / 2 ) >= i_m ) { w_l += histogram [ i_m + 1 ]; w_r -= histogram [ i_m + 1 ]; i_m ++ ; } } } return i_m ; }以下は、 Python言語での実装例です。
def balanced_histogram_thresholding ( histogram , minimum_bin_count : int = 5 , jump : int = 1 ) -> int : """ 画像のヒストグラムのバランスを取り、 重要なヒストグラムビンに焦点を当てて画像を 2 つの部分に分割することにより、最適な閾値を決定します。 引数: histogram (リスト): 画像のヒストグラムを整数のリストとして指定します。各要素は、 特定の強度レベルの ピクセル数を表します。 minimum_bin_count (int): しきい値処理で考慮されるビンの最小カウント 。この値より小さいカウントのビンは 無視され、ノイズの影響が軽減されます。 jump (int): 反復処理中にしきい値を調整するためのステップサイズ。値が大きいほど 収束が速くなりますが、最適なしきい値をスキップする可能性があります。 戻り値: int: 計算された閾値。この値は、 ヒストグラムの 重要な部分を前景 と背景として解釈できる 2 つのグループに最もよく分離する強度レベル (つまり、入力ヒストグラムのインデックス) を表します。 関数が -1 を返す場合、アルゴリズムが 制約内で適切な閾値を見つけることができなかったことを示します (たとえば、すべてのビンが minimum_bin_count 未満です)。 """ # ヒストグラムのビンが重要な開始インデックスと終了インデックスを見つけるstart_index = 0 while start_index < len ( histogram ) and histogram [ start_index ] < minimum_bin_count : start_index += 1end_index = len ( histogram ) - 1 while end_index >= 0 and histogram [ end_index ] < minimum_bin_count : end_index -= 1# 有効なビンが見つからないかどうかを確認します。if start_index >= end_index : return - 1 # エラーまたは適用外であることを示します# Initialize thresholdthreshold=(start_index+end_index)//2# Iteratively adjust the thresholdwhilestart_index<=end_index:# Calculate weights on both sides of the thresholdweight_left=sum(histogram[start_index:threshold])weight_right=sum(histogram[threshold:end_index+1])# Adjust the threshold based on the weightsifweight_left>weight_right:start_index+=jumpelifweight_left<weight_right:end_index-=jumpelse:# Equal weights; move both indicesstart_index+=jumpend_index-=jump# Calculate the new thresholdthreshold=(start_index+end_index)//2returnthreshold