How to obtain a gaussian filter in python

后端 未结 8 1365
广开言路
广开言路 2021-01-31 03:57

I am using python to create a gaussian filter of size 5x5. I saw this post here where they talk about a similar thing but I didn\'t find the exact way to get equivalent python c

8条回答
  •  别那么骄傲
    2021-01-31 04:18

    You could try this too (as product of 2 independent 1D Gaussian random variables) to obtain a 2D Gaussian Kernel:

    from numpy import pi, exp, sqrt
    s, k = 1, 2 #  generate a (2k+1)x(2k+1) gaussian kernel with mean=0 and sigma = s
    probs = [exp(-z*z/(2*s*s))/sqrt(2*pi*s*s) for z in range(-k,k+1)] 
    kernel = np.outer(probs, probs)
    print kernel
    
    #[[ 0.00291502  0.00792386  0.02153928  0.00792386  0.00291502]
    #[ 0.00792386  0.02153928  0.05854983  0.02153928  0.00792386]
    #[ 0.02153928  0.05854983  0.15915494  0.05854983  0.02153928]
    #[ 0.00792386  0.02153928  0.05854983  0.02153928  0.00792386]
    #[ 0.00291502  0.00792386  0.02153928  0.00792386  0.00291502]]
    
    import matplotlib.pylab as plt
    plt.imshow(kernel)
    plt.colorbar()
    plt.show()
    

提交回复
热议问题