iavarfilterf

Synopse

Variance filter.

  • g = iavarfilter(f, h)
    • g: Image.
    • f: Image. input image.
    • h: Image. kernel.

Description

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

Function Code

 1 from numpy import *
 2 
 3 def iavarfilter(f, h):
 4     from iapconv import iapconv
 5 
 6     f = asarray(f).astype(float64)
 7     f = f + 1e-320*(f == 0) # change zero by a very small number (prevent 'math range error')
 8     n = sum(ravel(h))
 9     fm = iapconv(f, h) / n
10     f2m = iapconv(f*f, h) / n
11     g = sqrt(f2m - (fm*fm)) / fm
12     return g

Examples

Numeric Example: constant example

1 from ia636 import iavarfilter, ianormalize
2 
3 f = ones((10,10)) # constant numeric example
4 g = iavarfilter(f, [[0,1,0],[1,1,1],[0,1,0]])
5 print 'Result image','\n',g
Result image 
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

Numeric Example 2

1 from ia636 import iavarfilter, ianormalize
2 
3 f = ones((7,7)) # constant numeric example
4 f[3,3] = 0
5 g = iavarfilter(f, [[1,1,1],[1,1,1],[1,1,1]])
6 print 'Result image','\n',g
Result image 
[[        NaN         NaN         NaN         NaN         NaN         NaN
          NaN]
 [        NaN         NaN         NaN         NaN         NaN         NaN
          NaN]
 [        NaN         NaN  0.35355339  0.35355339  0.35355339         NaN
          NaN]
 [        NaN         NaN  0.35355339  0.35355339  0.35355339         NaN
          NaN]
 [        NaN         NaN  0.35355339  0.35355339  0.35355339         NaN
          NaN]
 [        NaN         NaN         NaN         NaN         NaN         NaN
          NaN]
 [        NaN         NaN         NaN         NaN         NaN         NaN
          NaN]]

Image example 1:

1 from ia636 import iavarfilter, ianormalize
2 
3 f = adread('cameraman.pgm')
4 adshow(f)
5 g = iavarfilter(f, [[0,1,0],[1,1,1],[0,1,0]])
6 adshow(ianormalize(g))

Image example 2: mean kernel

1 from ia636 import iavarfilter, ianormalize
2 
3 adshow(ianormalize(f),'Original image')
4 h = [[1,1,1],[1,1,1],[1,1,1]]
5 g = iavarfilter(f,h)
6 adshow(ianormalize(g),'Variance Image')

Original image

Variance Image

Image example 3: kernel size

1 from ia636 import iavarfilter, ianormalize
2 
3 adshow(ianormalize(f),'Original image')
4 h = [[0,0,1,0,0],[0,0,1,0,0],[1,1,1,1,1],[0,0,1,0,0],[0,0,1,0,0]]
5 g = iavarfilter(f,h)
6 adshow(ianormalize(g),'Variance Image')

Original image

Variance Image

Image example 4: noise influence

1 from ia636 import iavarfilter, ianormalize
2 from exe12_function import *
3 
4 f = geraRuido(f,20)
5 adshow(f)
6 g = iavarfilter(f, [[1,1,1],[1,1,1],[1,1,1]])
7 adshow(ianormalize(g))

Image example 5: gaussian filter kernel

1 from ia636 import iavarfilter, ianormalize
2 from exe12_function import *
3 
4 f = geraRuido(f,20)
5 adshow(f)
6 h = [[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,7,4,1]]
7 g = iavarfilter(f, h)
8 adshow(ianormalize(g))

Image example 6: high pass filter kernel

1 from ia636 import iavarfilter, ianormalize
2 from exe12_function import *
3 
4 f = geraRuido(f,20)
5 adshow(f)
6 g = iavarfilter(f, [[-1,-1,-1],[-1,2,-1],[-1,-1,-1]])
7 adshow(ianormalize(g))

Equation

To futher explanation about sample variance check on: Variance