Function iagaussian

Synopse

Generate a d-dimensional Gaussian image.

  • g = iagaussian(s, mu, cov)
    • g: Image.
    • s: Image shape. (rows columns)
    • mu: Image. Mean vector. n-D point. Point of maximum value.
    • cov: Covariance matrix (symmetric and square).
01. import numpy as np
02. 
03. def iagaussian(s, mu, cov):
04.     d = len(s)  # dimension
05.     n = np.prod(s) # n. of samples (pixels)
06.     x = np.indices(s).reshape( (d, n))
07.     xc = x - mu
08.     k = 1. * xc * np.dot(np.linalg.inv(cov), xc)
09.     k = np.sum(k,axis=0) #the sum is only applied to the rows
10.     g = (1./((2 * np.pi)**(d/2.) * np.sqrt(np.linalg.det(cov)))) * np.exp(-1./2 * k)
11.     return g.reshape(s)

Description

A n-dimensional Gaussian image is an image with a Gaussian distribution. It can be used to generate test patterns or Gaussian filters both for spatial and frequency domain. The integral of the gaussian function is 1.0.

Examples

Example 1 - Numeric 2-dimensional

1. from ia636 import iagaussian
2. from ia636 import ianormalize
3. 
4. f = iagaussian((8,4), transpose([[3,1]]), [[1,0],[0,1]])
5. print 'f=\n', array2string(f, precision=4, suppress_small=1)
6. g = ianormalize(f, [0,255]).astype(uint8)
7. print 'g=\n', g
f=
[[ 0.0011  0.0018  0.0011  0.0002]
 [ 0.0131  0.0215  0.0131  0.0029]
 [ 0.0585  0.0965  0.0585  0.0131]
 [ 0.0965  0.1592  0.0965  0.0215]
 [ 0.0585  0.0965  0.0585  0.0131]
 [ 0.0131  0.0215  0.0131  0.0029]
 [ 0.0011  0.0018  0.0011  0.0002]
 [ 0.      0.0001  0.      0.    ]]
g=
[[  1   2   1   0]
 [ 20  34  20   4]
 [ 93 154  93  20]
 [154 255 154  34]
 [ 93 154  93  20]
 [ 20  34  20   4]
 [  1   2   1   0]
 [  0   0   0   0]]

Example 2 - one dimensional signal

1. # note that for 1-D case, the tuple has extra ,
2. # and the covariance matrix must be 2-D
3. f = iagaussian( (100,), 50, [[10*10]])
4. g = ianormalize(f, [0,1])
5. mmplot([[g]])

Example 3 - two-dimensional image

1. f = iagaussian((50,50), transpose([[25,10]]), [[10*10,0],[0,20*20]])
2. g = ianormalize(f, [0,255]).astype(uint8)
3. iashow(g)

Example 4 - Numeric 3-dimensional

1. f = iagaussian((3,4,5), transpose([[1,2,3]]), [[1,0,0],[0,4,0],[0,0,9]])
2. print 'f=\n', array2string(f, precision=4, suppress_small=1)
3. g = ianormalize(f, [0,255]).astype(uint8)
4. print 'g=\n', g
f=
[[[ 0.0024  0.0031  0.0037  0.0039  0.0037]
  [ 0.0034  0.0045  0.0054  0.0057  0.0054]
  [ 0.0039  0.0051  0.0061  0.0064  0.0061]
  [ 0.0034  0.0045  0.0054  0.0057  0.0054]]

 [[ 0.0039  0.0051  0.0061  0.0064  0.0061]
  [ 0.0057  0.0075  0.0088  0.0093  0.0088]
  [ 0.0064  0.0085  0.01    0.0106  0.01  ]
  [ 0.0057  0.0075  0.0088  0.0093  0.0088]]

 [[ 0.0024  0.0031  0.0037  0.0039  0.0037]
  [ 0.0034  0.0045  0.0054  0.0057  0.0054]
  [ 0.0039  0.0051  0.0061  0.0064  0.0061]
  [ 0.0034  0.0045  0.0054  0.0057  0.0054]]]
g=
[[[  0  23  40  47  40]
  [ 33  67  92 102  92]
  [ 47  86 115 125 115]
  [ 33  67  92 102  92]]

 [[ 47  86 115 125 115]
  [102 158 200 216 200]
  [125 189 237 254 237]
  [102 158 200 216 200]]

 [[  0  23  40  47  40]
  [ 33  67  92 102  92]
  [ 47  86 115 125 115]
  [ 33  67  92 102  92]]]

Equation

See Also

For a better understandig of this coding, consult ia636:iagengauss.