Balanced histogram thresholding: Difference between revisions

Content deleted Content added
external link doesn't work, fix refs
Fix issue with python code
 
(33 intermediate revisions by 27 users not shown)
Line 1:
{{short description|Type of image thresholding}}
In [[image processing]], the '''balanced histogram thresholding method''' (BHT),<ref>A. Anjos and H. Shahbazkia. Bi-Level Image Thresholding - A Fast Method. BIOSIGNALS 2008. Vol:2. P:70-76.</ref>, is a very simple method used for automatic image [[Thresholding (image processing)|thresholding]]. Like [[Otsu's Method]]<ref>Nobuyuki Otsu (1979). "A threshold selection method from gray-level histograms". IEEE Trans. Sys., Man., Cyber. 9: 62–66.</ref> and the '''Iterative Selection ThesholdingThresholding Method''',<ref>Ridler TW, Calvard S. (1978) Picture thresholding using an iterative selection method, IEEE Trans. System, Man and Cybernetics, SMC-8: 630-632.</ref>, 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.
[[File:Lovely spider.jpeg|thumb | 200px | right | Original image.]]
[[File:Lovely spider BHT.jpeg|thumb | 200px | right | Thresholded image.]]
[[File:BhaProgress3.gif|thumb | 200px | Evolution of the method.]]
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.
 
Line 7 ⟶ 9:
 
==Algorithm==
The following listing, in [[C (programming language)|C]] notation, is a simplified version of the '''Balanced Histogram Thresholding''' method:
<syntaxhighlight lang="c">
<code>
int BHThreshold(int[] histogram) {
i_m = (int)((i_s + i_e) / 2.0f); // center of the weighing scale I_m
w_l = get_weight(i_s, i_m + 1, histogram); // weight on the left W_l
w_r = get_weight(i_m + 1, i_e + 1, histogram); // weight on the right W_r
while (i_s <= i_e) {
if (w_r > w_l) { // right side is heavier
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) { // left side is heavier
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;
}
}</code>
</syntaxhighlight>
 
The following, is a possible implementation in the [[Python (programming language)|Python]] language:
[[File:BhaProgress3.gif|thumb | 200px | Evolution of the method.]]
<syntaxhighlight lang="python">
This method may have problems when dealing with very noisy images, because the ''weighing scale'' may be misplaced. The problem can be minimized by ignoring the extremities of the histogram.<ref>A. Anjos, R. Leite, M. L. Cancela, H. Shahbazkia. MAQ – A Bioinformatics Tool for Automatic Macroarray Analysis. International Journal of Computer Applications. 2010. Number 7 - Article 1.</ref>
def balanced_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 significant
start_index = 0
while start_index < len(histogram) and histogram[start_index] < minimum_bin_count:
start_index += 1
end_index = len(histogram) - 1
while end_index >= 0 and histogram[end_index] < minimum_bin_count:
end_index -= 1
 
# Check if no valid bins are found
if start_index >= end_index:
return -1 # Indicates an error or non-applicability
 
# Initialize threshold
threshold = (start_index + end_index) // 2
 
# Iteratively adjust the threshold
while start_index <= end_index:
# Calculate weights on both sides of the threshold
weight_left = sum(histogram[start_index:threshold])
weight_right = sum(histogram[threshold:end_index + 1])
 
# Adjust the threshold based on the weights
if weight_left > weight_right:
start_index += jump
elif weight_left < weight_right:
end_index -= jump
else: # Equal weights; move both indices
start_index += jump
end_index -= jump
 
# Calculate the new threshold
threshold = (start_index + end_index) // 2
 
return threshold
</syntaxhighlight>
 
==References==
{{reflist}}
 
<!-- ==External links==
* [http://w3.ualg.pt/~aanjos/prototype/BHThresholdingBHThresholding_.jar ImageJ Plugin] {{Webarchive|url=https://web.archive.org/web/20131017205614/http://w3.ualg.pt/~aanjos/prototype/BHThresholding_.jar |date=2013-10->17 }}
* [https://www.youtube.com/watch?v=rKWK4O4dZQ8 Otsu ''vs''. BHT]
 
{{DEFAULTSORT:Balanced Histogram Thresholding}}
[[Category:Image processingsegmentation]]
[[Category:Articles with example Python (programming language) code]]
 
[[fa:آستانه گذاری هیستوگرام متقارن]]
[[pt:Limiarização por equilíbrio do histograma]]
[[ru:Алгоритм сбалансированного порогового отсечения гистограммы]]