# Lesson iavarfilter

Autor: | marianapbento |
---|---|

Data: | 11/05/2011 |

Computes the variance on the neighborhood of the pixel. The neighborhood is given by the set marked by the kernel elements.

# Read the input image and kernel

```
1 from numpy import *
2 from ia636 import iapconv, ianormalize, iavarfilter
3
4 f = adread('cameraman.pgm')
5 f = asarray(f).astype(float64)
6 f = f + 1e-320*(f == 0) # change zero by a very small number (prevent 'math range error')
7 adshow(f,'Original image')
8 h = [[0,1,0],[1,1,1],[0,1,0]]
```

# Compute the mean on the neighborhood of the pixel

```
1 # calculate the normalization factor n
2 n = sum(ravel(h))
3 print 'Normalization factor:',n
4 # calculate the periodic convolution between f and h
5 fm = iapconv(f, h) / n
6 adshow(fm,'periodic convolution between f and h')
```

Normalization factor: 5

# Compute the square mean on the neighborhood of the pixel

```
1 # shows f*f result
2 adshow(ianormalize(f*f),'f*f')
3 # calculate the periodic convolution between f*f and h
4 f2m = iapconv(f*f, h) / n
5 adshow(ianormalize(f2m),'Periodic convolution between f*f and h')
```

# Compute and visualize the variance on the neighborhood of the pixel

```
1 var = f2m - (fm*fm)
2 adshow(ianormalize(var),'Neighborhood variance')
3 desv = sqrt(var)
4 adshow(ianormalize(desv),'Neighborhood Standard Deviation')
5 g = desv / fm
6 adshow(ianormalize(g),'Final Result')
```