Balanced histogram thresholding: Difference between revisions

Content deleted Content added
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5
No edit summary
Line 37:
The following, is a possible implementation in the [[Python (programming language)|Python]] language:
<syntaxhighlight lang="python">
import numpy as np
def bht(hist, min_count: int = 5) -> int:
"""Balanced histogram thresholding."""
n_bins = len(hist) # assumes 1D histogram
h_s = 0
while hist[h_s] < min_count:
h_s += 1 # ignore small counts at start
h_e = n_bins - 1
while hist[h_e] < min_count:
h_e -= 1 # ignore small counts at end
# use mean intensity of histogram as center; alternatively: (h_s + h_e) / 2)
h_c = int(round(np.average(np.linspace(0, 2 ** 8 - 1, n_bins), weights=hist)))
w_l = np.sum(hist[h_s:h_c]) # weight in the left part
w_r = np.sum(hist[h_c : h_e + 1]) # weight in the right part
 
def balanced_histogram_thresholding(histogram, minimum_bin_count: int = 5) -> int:
while h_s < h_e:
"""
if w_l > w_r: # left part became heavier
Determines an optimal threshold by balancing the histogram of an image,
w_l -= hist[h_s]
focusing on significant histogram bins to segment the image into two parts.
h_s += 1
else: # right part became heavier
w_r -= hist[h_e]
h_e -= 1
new_c = int(round((h_e + h_s) / 2)) # re-center the weighing scale
 
This function iterates through the histogram to find a threshold that divides
if new_c < h_c: # move bin to the other side
the histogram into two parts with a balanced sum of bin counts on each side.
w_l -= hist[h_c]
It effectively segments the image into foreground and background based on this threshold.
w_r += hist[h_c]
The algorithm ignores bins with counts below a specified minimum, ensuring that
elif new_c > h_c:
noise or very low-frequency bins do not affect the thresholding process.
w_l += hist[h_c]
w_r -= hist[h_c]
 
h_c = new_cArgs:
histogram (np.ndarray): The histogram of the image as a 1D numpy array,
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.
 
return h_cReturns:
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).
 
"""
h_sstart_index = 0
while histogram[start_index] < minimum_bin_count and start_index < len(histogram) - 1:
h_sstart_index += 1
end_index = len(histogram) - 1
while histogram[end_index] < minimum_bin_count and end_index > 0:
h_eend_index -= 1
 
if start_index >= end_index:
return -1 # Indicates an error or non-applicability
 
threshold = (start_index + end_index) // 2
 
while h_s < h_eTrue:
weight_left = np.sum(histogram[start_index:threshold])
weight_right = np.sum(histogram[threshold:end_index + 1])
 
if weight_left > weight_right:
end_index = threshold - 1
else:
start_index = threshold + 1
 
new_threshold = (start_index + end_index) // 2
 
if new_threshold == threshold:
w_l -= hist[h_s]break
else:
threshold = new_threshold
 
return threshold
</syntaxhighlight>