Balanced histogram thresholding


In image processing, the balanced histogram thresholding method, is a very simple method used for automatic image thresholding. Like Otsu's Method and the Iterative Selection Thresholding Method, 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.

Algorithm

The following listing, in C notation, is a simplified version of the Balanced Histogram Thresholding method:

int BHThreshold

The following, is a possible implementation in the Python language:

def bht -> int:
"""Balanced histogram thresholding."""
n_bins = len # assumes 1D histogram
h_s = 0
while hist < min_count:
h_s += 1 # ignore small counts at start
h_e = n_bins - 1
while hist < min_count:
h_e -= 1 # ignore small counts at end
# use mean intensity of histogram as center; alternatively:
h_c = int)
w_l = np.sum # weight in the left part
w_r = np.sum # weight in the right part
while h_s < h_e:
if w_l > w_r: # left part became heavier
w_l -= hist
h_s += 1
else: # right part became heavier
w_r -= hist
h_e -= 1
new_c = int) # re-center the weighing scale
if new_c < h_c: # move bin to the other side
w_l -= hist
w_r += hist
elif new_c > h_c:
w_l += hist
w_r -= hist
h_c = new_c
return h_c