def otsu_threshold(data, bins=<NUM_LIT:255>):
h0, x0 = np.histogram(data.ravel(), bins=bins)<EOL>h = h0.astype('<STR_LIT:float>') / h0.sum()  <EOL>x = <NUM_LIT:0.5>*(x0[<NUM_LIT:1>:] + x0[:-<NUM_LIT:1>])  <EOL>wk = np.array([h[:i+<NUM_LIT:1>].sum() for i in range(h.size)])  <EOL>mk = np.array([sum(x[:i+<NUM_LIT:1>]*h[:i+<NUM_LIT:1>]) for i in range(h.size)])  <EOL>mt = mk[-<NUM_LIT:1>]  <EOL>sb = (mt*wk - mk)**<NUM_LIT:2> / (wk*(<NUM_LIT:1>-wk) + <NUM_LIT>)  <EOL>ind = sb.argmax()<EOL>return <NUM_LIT:0.5>*(x0[ind] + x0[ind+<NUM_LIT:1>])<EOL>
Otsu threshold on data.

Otsu thresholding [1]_is a method for selecting an intensity value
for thresholding an image into foreground and background. The sel-
ected intensity threshold maximizes the inter-class variance.

Parameters
----------
    data : numpy.ndarray
        The data to threshold
    bins : Int or numpy.ndarray, optional
        Bin edges, as passed to numpy.histogram

Returns
-------
    numpy.float
        The value of the threshold which maximizes the inter-class
        variance.

Notes
-----
    This could be generalized to more than 2 classes.
References
----------
    ..[1] N. Otsu, "A Threshold Selection Method from Gray-level
        Histograms," IEEE Trans. Syst., Man, Cybern., Syst., 9, 1,
        62-66 (1979)