Gaussiana

Autor: jahuaman
Data: 28/06/2009

Generate a 2D Gaussian image

  • g = iagaussian2(s, mu, sigma)
    • g: Image.
    • s: Image. [rows columns]
    • mu: Image. Mean vector. 2D point (x;y). Point of maximum value.
    • sigma: Image. covariance matrix (square). [ Sx^2 Sxy; Syx Sy^2]
 1 from ia636 import iaind2sub
 2 from numpy import *
 3 from numpy.linalg import *
 4 
 5 def iagaussian2(s, mu, sigma):
 6 
 7     #if type(sigma).__name__ in ['int', 'float', 'complex']: sigma = [sigma]
 8     s, mu, sigma = asarray(s), asarray(mu), asarray(sigma)
 9     n = s.size # n: dimensão
10 
11     if (n == 1):  # 1-D case
12         x = arange(s)
13         d = x - mu
14         k = (1. * d * d) /sigma
15 
16     else:         # n-D case
17         # the lines below generates the coordinate matrix for the dimension "n" provided in s
18         n = s.size
19         x = zeros((prod(s),n))
20         x[:,n-1] = arange(prod(s))
21         for i in range(1,n):
22             aux=x[:,n-i]
23             x[:,n-i-1],x[:,n-i]=iaind2sub([s[n-i-1],s[n-i]],aux)
24         x=x.astype(uint8)
25         #
26         d = x - resize(reshape(mu,(mu.size,1)), (prod(s),mu.size))
27 
28         if sigma.size == 1:
29             tmp = 1./sigma
30         else:
31             tmp = inv(sigma)
32 
33         k = dot(d, tmp) * d
34         k = sum(k,axis=1)
35 
36     g = zeros(s, float)
37     aux = ravel(g)
38     if sigma.size == 1:
39         tmp = sigma
40     else:
41         tmp = det(sigma)
42     aux[:] = 1./(2*pi*sqrt(tmp)) * exp(-1./2 * k)
43     return g

Description

A 2D 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

1 from ia636 import iagaussian
2 from ia636 import ianormalize
3 
4 f = iagaussian2([8,4], [3,1], [[1,0],[0,1]])
5 print 'f=\n', f
6 print array2string(f, precision=4, suppress_small=1)
7 g = ianormalize(f, [0,255]).astype(uint8)
8 print g
f=
[[  1.07237757e-03   1.76805171e-03   1.07237757e-03   2.39279779e-04]
 [  1.30642333e-02   2.15392793e-02   1.30642333e-02   2.91502447e-03]
 [  5.85498315e-02   9.65323526e-02   5.85498315e-02   1.30642333e-02]
 [  9.65323526e-02   1.59154943e-01   9.65323526e-02   2.15392793e-02]
 [  5.85498315e-02   9.65323526e-02   5.85498315e-02   1.30642333e-02]
 [  1.30642333e-02   2.15392793e-02   1.30642333e-02   2.91502447e-03]
 [  1.07237757e-03   1.76805171e-03   1.07237757e-03   2.39279779e-04]
 [  3.23829967e-05   5.33905355e-05   3.23829967e-05   7.22562324e-06]]
[[ 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.    ]]
[[  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

1 f = iagaussian2(100, 50, 10*10)
2 g = ianormalize(f, [0,1])
3 mmplot([[g]])
4 # showfig(g)

Example 3

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

Example 4

3-D

1 f = iagaussian2([4,4,2], [1,1,0], 1*1*1)
2 print 'f=\n', f
3 print array2string(f, precision=4, suppress_small=1)
4 g = ianormalize(f, [0,255]).astype(uint8)
5 print g
f=
[[[ 0.05854983  0.03551227]
  [ 0.09653235  0.05854983]
  [ 0.05854983  0.03551227]
  [ 0.01306423  0.00792386]]

 [[ 0.09653235  0.05854983]
  [ 0.15915494  0.09653235]
  [ 0.09653235  0.05854983]
  [ 0.02153928  0.01306423]]

 [[ 0.05854983  0.03551227]
  [ 0.09653235  0.05854983]
  [ 0.05854983  0.03551227]
  [ 0.01306423  0.00792386]]

 [[ 0.01306423  0.00792386]
  [ 0.02153928  0.01306423]
  [ 0.01306423  0.00792386]
  [ 0.00291502  0.00176805]]]
[[[ 0.0585  0.0355]
  [ 0.0965  0.0585]
  [ 0.0585  0.0355]
  [ 0.0131  0.0079]]

 [[ 0.0965  0.0585]
  [ 0.1592  0.0965]
  [ 0.0965  0.0585]
  [ 0.0215  0.0131]]

 [[ 0.0585  0.0355]
  [ 0.0965  0.0585]
  [ 0.0585  0.0355]
  [ 0.0131  0.0079]]

 [[ 0.0131  0.0079]
  [ 0.0215  0.0131]
  [ 0.0131  0.0079]
  [ 0.0029  0.0018]]]
[[[ 91  54]
  [153  91]
  [ 91  54]
  [ 18   9]]

 [[153  91]
  [255 153]
  [153  91]
  [ 32  18]]

 [[ 91  54]
  [153  91]
  [ 91  54]
  [ 18   9]]

 [[ 18   9]
  [ 32  18]
  [ 18   9]
  [  1   0]]]

Example 5

1 f = iagaussian2([100,100], [50,50], 20*20)
2 g = ianormalize(f, [0,255]).astype(uint8)
3 iashow(g)

Equation