Content deleted Content added
No edit summary |
→Algorithm: PEP-8 |
||
Line 38:
The following, is a possible implementation in the [[Python (programming language)|Python]] language:
<syntaxhighlight lang="python">
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 -= # 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
while h_s < h_e:
if w_l > w_r: # left part became heavier
w_l -= hist[h_s]
Line 64 ⟶ 67:
w_l += hist[h_c]
w_r -= hist[h_c]
h_c = new_c
|