# Function iaotsu

namespace: ia636 iaotsu

# Synopse

Thresholding by Otsu.

• t, eta = iaotsu(f)
• t, eta: Double.
• f: Image. input image.
```01. from numpy import *
02.
03. def iaotsu(f):
04.     n = product(shape(f))
05.     h = 1.*bincount(f.ravel()) / n  # used bincount instead of iahistogram
06.     if len(h) == 1: return 1,1
07.     x = arange(product(shape(h)))
08.     w0 = cumsum(h)
09.     w1 = 1 - w0
10.     eps = 1e-10
11.     m0 = cumsum(x * h) / (w0 + eps)
12.     mt = m0[-1]
13.     m1 = (mt - m0[0:-1]*w0[0:-1]) / w1[0:-1]
14.     sB2 = w0[0:-1] * w1[0:-1] * ((m0[0:-1] - m1)**2)
15.     t = argmax(sB2)
16.     v = sB2[t]
17.     st2 = sum((x-mt)**2 * h)
18.     eta = v / st2
19.     return t, eta```

# Description

Compute the automatic thresholding level of a gray scale image based on the Otsu method.

# Examples

## Numerical

```1. from ia636 import iaotsu
2. f = arange(25).reshape((5,5))
3. print f
4. t,_ = iaotsu(f)
5. print t```
```[[ 0  1  2  3  4]
[ 5  6  7  8  9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]]
12
```

## Extremes cases

```1. f = ones((5,5),'uint8')
2. t,_ = iaotsu(f)
3. print t
4.
5. f = zeros((5,5),'uint8')
6. t,_ = iaotsu(f)
7. print t```
```0
1
```

## Example 1

```1. from ia636 import iaotsu
2.
4. iashow(f)
5. (t, eta) = iaotsu(f)
6. print 'threshold at %f, goodness=%f\n' %(t, eta)
7. iashow(f > t)```
```threshold at 90.000000, goodness=0.938940
```

## Measuring time performance

```1. import time
3. g = vstack((f,f))
4. h = hstack((g,g))
5. t = time.time()
6. thres, eta = iaotsu(h)
7. tend = time.time()
8. print 'Computational time %s is %f seconds.' % (h.shape, (tend - t))```
```Computational time (512, 512) is 0.003468 seconds.
```

# Reference

• Nobuyuki Otsu (1979). "A threshold selection method from gray-level histograms". IEEE Trans. Sys., Man., Cyber. 9 (1): 62–66. doi:10.1109/TSMC.1979.4310076.
• Wikipedia Otsu's method