In [1]:
import numpy as np

def gauss2D(shape=(3,3),sigma=0.5):
    """
    2D gaussian mask - should give the same result as MATLAB's
    fspecial('gaussian',[shape],[sigma])
    """
    m,n = [(ss-1.)/2. for ss in shape]
    y,x = np.ogrid[-m:m+1,-n:n+1]
    h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )
    h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
    sumh = h.sum()
    if sumh != 0:
        h /= sumh
    return h

In [2]:
gauss2D((5,5), 5)


Out[2]:
array([[ 0.03688345,  0.03916419,  0.03995536,  0.03916419,  0.03688345],
       [ 0.03916419,  0.04158597,  0.04242606,  0.04158597,  0.03916419],
       [ 0.03995536,  0.04242606,  0.04328312,  0.04242606,  0.03995536],
       [ 0.03916419,  0.04158597,  0.04242606,  0.04158597,  0.03916419],
       [ 0.03688345,  0.03916419,  0.03995536,  0.03916419,  0.03688345]])

In [ ]: