# Synopse

The iah2percentile function computes the percentile given an image histogram.

• g = iapercentile(h,q)
• Output
• g: percentile value(s)
• Input
• h: 1D ndarray: histogram
• q: 1D float ndarray in range of [0,100]. Default value = 1.

# Description

The iah2percentile function computes the percentiles from a given histogram.

# Function Code

```01. def iah2percentile(h,p):
02.
03.     import numpy as np
04.     s = h.sum()
05.     k = ((s-1) * p/100.)+1
06.     dw = np.floor(k)
07.     up = np.ceil(k)
08.     hc = np.cumsum(h)
09.     if isinstance(p, int):
10.        k1 = np.argmax(hc>=dw)
11.        k2 = np.argmax(hc>=up)
12.     else:
13.        k1 = np.argmax(hc>=dw[:,np.newaxis],axis=1)
14.        k2 = np.argmax(hc>=up[:,np.newaxis],axis=1)
15.     d0 = k1 * (up-k)
16.     d1 = k2 * (k -dw)
17.     return np.where(dw==up,k1,d0+d1)```

# Examples

## Numeric Example

Comparison with the NumPy percentile implementation:

```01. import numpy as np
02. import ia636 as ia
03.
04. f = np.array([0,1,2,3,4,5,6,7,8])
05. h = ia.iahistogram(f)
06. print 'iah2percentile  1 = %f, np.percentile  1 = %f'%(ia.iah2percentile(h,1),np.percentile(f,1))
07. print 'iah2percentile 10 = %f, np.percentile 10 = %f'%(ia.iah2percentile(h,10),np.percentile(f,10))
08. print 'iah2percentile 50 = %f, np.percentile 50 = %f'%(ia.iah2percentile(h,50),np.percentile(f,50))
09. print 'iah2percentile 90 = %f, np.percentile 90 = %f'%(ia.iah2percentile(h,90),np.percentile(f,90))
10. print 'iah2percentile 99 = %f, np.percentile 99 = %f'%(ia.iah2percentile(h,99),np.percentile(f,99))```
```iah2percentile  1 = 0.080000, np.percentile  1 = 0.080000
iah2percentile 10 = 0.800000, np.percentile 10 = 0.800000
iah2percentile 50 = 4.000000, np.percentile 50 = 4.000000
iah2percentile 90 = 7.200000, np.percentile 90 = 7.200000
iah2percentile 99 = 7.920000, np.percentile 99 = 7.920000
```
```1. f = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
2. h = ia.iahistogram(f)
3. p = [1, 10, 50, 90, 99]
4. print 'percentiles:', p
5. print 'iah2percentile', ia.iah2percentile(h,array(p))
6. print 'np.percentile', np.percentile(f,p)```
```percentiles: [1, 10, 50, 90, 99]
iah2percentile [ 0.09  0.9   4.5   8.1   8.91]
np.percentile [ 0.09  0.9   4.5   8.1   8.91]
```

## Image Example

```1. f = adread('cameraman.tif')
2. h = ia.iahistogram(f)
3. p = [1, 10, 50, 90, 99]
4. print 'percentiles:', p
5. print 'iah2percentile', ia.iah2percentile(h,array(p))
6. print 'np.percentile', np.percentile(f,p)
7. print 'median', median(f)```
```percentiles: [1, 10, 50, 90, 99]
iah2percentile [   2.    8.  180.  222.  251.]
np.percentile [   2.    8.  180.  222.  251.]
median 180.0
```