# Synopse

Compute variance or the coefficient of variation on a specified neighborhood for every imagem pixel.

• g = iavarfilter(f,h,CV)
• OUTPUT
• g: Image. The neighborhood coefficient of variation for each image pixel.
• INPUT
• f: Image. The input image.
• h: Matrix. The kernel. Is a binary matrix where values 1 denotes a neighbor of the pixel at central element.
• CV: Boolean. Default: True. Specify the result type, which are the coefficient of variation (CV) if True, or the variance otherwise.

# Description

Computes the variance or the coefficient of variation on the neighborhood of the image pixels. The neighborhood is given by the set marked by the kernel elements.

``` 1 from numpy import *
2
3 def iavarfilter(f,h,CV=True):
4
5     from ia636 import iapconv
6
7     f = asarray(f).astype(float)
8     h = asarray(h).astype(bool)[::-1,::-1].astype(float)
9
10     h /= h.sum()
11     fm = iapconv(f, h)
12     f2m = iapconv(f**2, h)
13
14     g = f2m - fm**2
15
16     if (CV):
17         fm = fm + 1e-32 *(fm == 0) # change zero by a very small number (prevent 'math range error')
18         g[g<0.0] = 0.0 # avoid negative numbers due to numeric precision
19         g = sqrt(g) / fm
20
21     return g
```

# Examples

## Example 1

``` 1 import numpy as np
2 import ia636 as ia
3
4 f = np.array(
5 [[5,5,5,0,0,0],
6  [5,5,5,0,0,0],
7  [0,0,1,1,1,0],
8  [0,0,1,1,1,0],
9  [0,0,0,9,9,9],
10  [0,0,0,9,9,9]])
11 h = np.array([[1,1,1],
12               [1,1,1]])
13 s = ia.iavarfilter(f, h, False)
14 cv= ia.iavarfilter(f, h)
15
16 print "f:\n",f
17 print "h:\n",h
18 print "s:\n",s.round(2)
19 print "cv:\n",cv.round(2)
```
```f:
[[5 5 5 0 0 0]
[5 5 5 0 0 0]
[0 0 1 1 1 0]
[0 0 1 1 1 0]
[0 0 0 9 9 9]
[0 0 0 9 9 9]]
h:
[[1 1 1]
[1 1 1]]
s:
[[ 11.81   6.25  11.81  16.47  20.25  16.47]
[  5.56   0.     5.56   5.56   0.     5.56]
[  5.56   5.56   4.67   2.89   0.22   3.33]
[  0.     0.22   0.22   0.     0.22   0.22]
[ 11.25   0.14  10.47  15.25  17.47  17.14]
[ 18.     0.    18.    18.     0.    18.  ]]
cv:
[[ 1.09  1.    1.09  1.06  1.    1.06]
[ 0.71  0.    0.71  1.41  0.    1.41]
[ 1.41  0.88  1.08  1.27  1.41  1.83]
[ 0.    1.41  0.71  0.    0.71  1.41]
[ 2.24  2.24  1.77  1.12  0.86  1.31]
[ 1.41  0.    1.41  0.71  0.    0.71]]
```

## Example 2

``` 1 f = adreadgray('cameraman.pgm')
2 h = np.array([[0,1,0],
3               [1,1,1],
4               [0,1,0]])
5 print "Kernel:\n",h
7 g = ia.iavarfilter(f, h, False)
9 g = ia.iavarfilter(f, h)
10 adshow(ia.ianormalize(g), "Resulting image (coefficient of variation)")
```
```Kernel:
[[0 1 0]
[1 1 1]
[0 1 0]]
```

# Equation

The following equations computes, respectively, the variance (Var) and the coefficient of variation (CV):

where is an image; is the mean of ; is the total number of elements in ; and is a pixel in .

## On the implementation ...

These equations are applied on the neighborhood of image pixels. Let be a matrix that define a neighborhood relation between image pixels, the algorithm calculates the equations above throught convolution operations

and

Finally, for variance we have

and for the coefficient of variation