Content deleted Content added
→Algorithm: rm unreliable source |
No edit summary |
||
Line 31:
return i_m;
}
</syntaxhighlight>
The following, is a possible implementation in the [[Python (programming language)|Python]] language:
<syntaxhighlight lang="python">
def bht(hist, min_count=5):
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
while h_s < h_e:
if w_l > w_r: # left part became heavier
w_l -= hist[h_s]
h_s += 1
else: # right part became heavier
w_r -= hist[h_e]
h_e -= 1
h_c = int(round((h_e + h_s)/2)) # re-center the weighing scale
return h_c
</syntaxhighlight>
|