# Synopse

The iapercentile function computes the percentile given an image.

• g = iapercentile(f,p)
• Output
• g: 1D float ndarray: percentile values
• Input
• f: 2D ndarray: image
• p: 1D integer ndarray: in range of [0,100].Percentile to compute which must be between 0 and 100 inclusive. Default value = 1

# Description

The iapercentile function computes the percentiles from a given histogram. It uses the interpolated percentil.

# Function Code

```01. def iapercentile(f, p=1):
02.
03.    import numpy as np
04.    k = (f.size-1) * p/100.
05.    dw = np.floor(k).astype(int)
06.    up = np.ceil(k).astype(int)
07.    g  = np.sort(f.ravel())
08.    d  = g[dw]
09.    d0 =   d   * (up-k)
10.    d1 = g[up] * (k -dw)
11.    return np.where(dw==up, d, d0+d1)```

# Examples

## Numeric Example

Comparison with the NumPy percentile implementation:

```1. import numpy as np
2. import ia636 as ia
3.
4. f = np.array([0,1,2,3,4,5,6,7,8])
5. print 'iapercentile  1 = %f, np.percentile  1 = %f'%(ia.iapercentile(f,1), np.percentile(f,1))
6. print 'iapercentile 10 = %f, np.percentile 10 = %f'%(ia.iapercentile(f,10),np.percentile(f,10))
7. print 'iapercentile 50 = %f, np.percentile 50 = %f'%(ia.iapercentile(f,50),np.percentile(f,50))
8. print 'iapercentile 90 = %f, np.percentile 90 = %f'%(ia.iapercentile(f,90),np.percentile(f,90))
9. print 'iapercentile 99 = %f, np.percentile 99 = %f'%(ia.iapercentile(f,99),np.percentile(f,99))```
```iapercentile  1 = 0.080000, np.percentile  1 = 0.080000
iapercentile 10 = 0.800000, np.percentile 10 = 0.800000
iapercentile 50 = 4.000000, np.percentile 50 = 4.000000
iapercentile 90 = 7.200000, np.percentile 90 = 7.200000
iapercentile 99 = 7.920000, np.percentile 99 = 7.920000
```
```1. f = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
2. print 'iapercentile  1 = %f, np.percentile  1 = %f'%(ia.iapercentile(f,1), np.percentile(f,1))
3. print 'iapercentile 10 = %f, np.percentile 10 = %f'%(ia.iapercentile(f,10),np.percentile(f,10))
4. print 'iapercentile 50 = %f, np.percentile 50 = %f'%(ia.iapercentile(f,50),np.percentile(f,50))
5. print 'iapercentile 90 = %f, np.percentile 90 = %f'%(ia.iapercentile(f,90),np.percentile(f,90))
6. print 'iapercentile 99 = %f, np.percentile 99 = %f'%(ia.iapercentile(f,99),np.percentile(f,99))```
```iapercentile  1 = 0.090000, np.percentile  1 = 0.090000
iapercentile 10 = 0.900000, np.percentile 10 = 0.900000
iapercentile 50 = 4.500000, np.percentile 50 = 4.500000
iapercentile 90 = 8.100000, np.percentile 90 = 8.100000
iapercentile 99 = 8.910000, np.percentile 99 = 8.910000
```

## Image Example

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

# Equation

In the follow equation, denotes the value intensity in the image ( usually between 0 and 255) where p % of its pixels are below this value (100-p)% are above this value. The percentile used is the interpolated value. The median is the percentile 50.