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:
intBHThreshold(int[]histogram){i_m=(int)((i_s+i_e)/2.0f);// center of the weighing scale I_mw_l=get_weight(i_s,i_m+1,histogram);// weight on the left W_lw_r=get_weight(i_m+1,i_e+1,histogram);// weight on the right W_rwhile(i_s<=i_e){if(w_r>w_l){// right side is heavierw_r-=histogram[i_e--];if(((i_s+i_e)/2)<i_m){w_r+=histogram[i_m];w_l-=histogram[i_m--];}}elseif(w_l>=w_r){// left side is heavierw_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++;}}}returni_m;}
The following, is a possible implementation in the Python language:
defbalanced_histogram_thresholding(histogram,minimum_bin_count:int=5,jump:int=1)->int:""" Determines an optimal threshold by balancing the histogram of an image, focusing on significant histogram bins to segment the image into two parts. Args: histogram (list): The histogram of the image as a list of integers, where each element represents the count of pixels at a specific intensity level. minimum_bin_count (int): Minimum count for a bin to be considered in the thresholding process. Bins with counts below this value are ignored, reducing the effect of noise. jump (int): Step size for adjusting the threshold during iteration. Larger values speed up convergence but may skip the optimal threshold. Returns: int: The calculated threshold value. This value represents the intensity level (i.e. the index of the input histogram) that best separates the significant parts of the histogram into two groups, which can be interpreted as foreground and background. If the function returns -1, it indicates that the algorithm was unable to find a suitable threshold within the constraints (e.g., all bins are below the minimum_bin_count). """# Find the start and end indices where the histogram bins are significantstart_index=0whilestart_index<len(histogram)andhistogram[start_index]<minimum_bin_count:start_index+=1end_index=len(histogram)-1whileend_index>=0andhistogram[end_index]<minimum_bin_count:end_index-=1# Check if no valid bins are foundifstart_index>=end_index:return-1# Indicates an error or non-applicability# 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