Function iavarfilter

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.

01. from numpy import *
02. 
03. def iavarfilter(f,h,CV=True):
04. 
05.     from ia636 import iapconv
06. 
07.     f = asarray(f).astype(float64)
08.     h = asarray(h).astype(bool)[::-1,::-1]
09. 
10.     n = sum(ravel(h))
11.     h = h/float(n)
12.     fm = iapconv(f, h)
13.     f2m = iapconv(f**2, h)
14. 
15.     g = f2m - fm**2
16. 
17.     if (CV):
18.         fm = fm + 1e-320*(fm == 0) # change zero by a very small number (prevent 'math range error')
19.         g[g<0.0] = 0.0 # avoid negative numbers due to numeric precision
20.         g = sqrt(g) / fm
21. 
22.     return g

Examples

Example 1

01. from numpy import *
02. from ia636 import *
03. 
04. f = adreadgray('cameraman.pgm')
05. h = array([[0,1,0],
06.            [1,1,1],
07.            [0,1,0]])
08. print "Kernel:\n",h
09. adshow(f, "Original image")
10. g = iavarfilter(f, h, False)
11. adshow(ianormalize(g), "Resulting image (variance)")
12. g = iavarfilter(f, h)
13. adshow(ianormalize(g), "Resulting image (coefficient of variation)")
Kernel:
[[0 1 0]
 [1 1 1]
 [0 1 0]]

Original image

Resulting image (variance)

Resulting image (coefficient of variation)

Example 2

01. f = array(
02. [[0,0,0,0,0,0],
03.  [0,0,0,0,0,0],
04.  [0,0,9,9,0,0],
05.  [0,0,9,9,0,0],
06.  [0,0,0,0,1,0],
07.  [0,0,0,0,0,0]])
08. 
09. h = array([[0,1,0],
10.               [1,1,0],
11.               [0,0,1]])
12. 
13. g = iavarfilter(f, h)
14. 
15. print "Original image:\n",f,"\n\n"
16. print "Kernel:\n",h,"\n\n"
17. print "Resulting image (coefficient of variation):\n",g,"\n\n"
18. g = iavarfilter(f, h, False)
19. print "Resulting image (variance):\n",g,"\n\n"
Original image:
[[0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 9 9 0 0]
 [0 0 9 9 0 0]
 [0 0 0 0 1 0]
 [0 0 0 0 0 0]] 


Kernel:
[[0 1 0]
 [1 1 0]
 [0 0 1]] 


Resulting image (coefficient of variation):
[[ 0.          0.          0.          0.          0.          0.        ]
 [ 0.          1.73205081  1.73205081  0.          0.          0.        ]
 [ 0.          1.73205081  1.          1.          1.73205081  0.        ]
 [ 0.          0.          1.          0.49487166  1.73205081  0.        ]
 [ 0.          0.          1.73205081  1.73205081  1.73205081  1.73205081]
 [ 0.          0.          0.          0.          1.73205081  0.        ]] 


Resulting image (variance):
[[  0.       0.       0.       0.       0.       0.    ]
 [  0.      15.1875  15.1875   0.       0.       0.    ]
 [  0.      15.1875  20.25    20.25    15.1875   0.    ]
 [  0.       0.      20.25    12.      15.1875   0.    ]
 [  0.       0.      15.1875  15.1875   0.1875   0.1875]
 [  0.       0.       0.       0.       0.1875   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

See also

Contributions

  • André Luis da Costa, 1st semester 2011