The h2percentile function computes the percentile given an image histogram.
g = iapercentile(h,q)
Output
Input
The h2percentile function computes the percentiles from a given histogram.
In [ ]:
    
def h2percentile(h,p):
    import numpy as np
    s = h.sum()
    k = ((s-1) * p/100.)+1
    dw = np.floor(k)
    up = np.ceil(k)
    hc = np.cumsum(h)
    if isinstance(p, int):
        k1 = np.argmax(hc>=dw)
        k2 = np.argmax(hc>=up)
    else:   
        k1 = np.argmax(hc>=dw[:,np.newaxis],axis=1)
        k2 = np.argmax(hc>=up[:,np.newaxis],axis=1)
    d0 = k1 * (up-k)
    d1 = k2 * (k -dw)
    return np.where(dw==up,k1,d0+d1)
    
In [1]:
    
testing = (__name__ == "__main__")
if testing:
    ! jupyter nbconvert --to python h2percentile.ipynb
    import numpy as np
    import sys,os
    import matplotlib.image as mpimg
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia
    
    
In [2]:
    
if testing:
    f = np.array([0,1,2,3,4,5,6,7,8])
    h = ia.histogram(f)
    print('h2percentile  1 = %f, np.percentile  1 = %f'%(ia.h2percentile(h,1),np.percentile(f,1)))
    print('h2percentile 10 = %f, np.percentile 10 = %f'%(ia.h2percentile(h,10),np.percentile(f,10)))
    print('h2percentile 50 = %f, np.percentile 50 = %f'%(ia.h2percentile(h,50),np.percentile(f,50)))
    print('h2percentile 90 = %f, np.percentile 90 = %f'%(ia.h2percentile(h,90),np.percentile(f,90)))
    print('h2percentile 99 = %f, np.percentile 99 = %f'%(ia.h2percentile(h,99),np.percentile(f,99)))
    
    
In [3]:
    
if testing:
    f = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    h = ia.histogram(f)
    p = [1, 10, 50, 90, 99]
    print('percentiles:', p)
    print('h2percentile', ia.h2percentile(h,np.array(p)))
    print('np.percentile', np.percentile(f,p))
    
    
In [4]:
    
if testing:
    import matplotlib.image as mpimg
    f = mpimg.imread('../data/cameraman.tif')  
    h = ia.histogram(f)
    p = [1, 10, 50, 90, 99]
    print('percentiles:', p)
    print('h2percentile', ia.h2percentile(h,np.array(p)))
    print('np.percentile', np.percentile(f,p))
    print('median', np.median(f))
    
    
ia636:iahistogram iahistogramia636:iapercentile - Computes the percentile from the imagehttp://docs.scipy.org/doc/scipy/reference/stats.html SciPy Statshttp://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.percentile.html numpy percentile
In [ ]: