Atul Singh
Atul Singh

Reputation: 19

How to implement Gaussian filter of kernel size 3

Can someone please help me in computing the Gaussian filter values? I have seen other related posts but was unable to figure out the proper solution. I have the 2D gaussian equation:

def gauss2d(x,y,sigma):
    return (1/(2*math.pi*math.pow(sigma,2)))*math.exp(-0.5*
                           (pow(x,2)+pow(y,2))/pow(sigma,2))  

I want to figure out how to compute the elements of a 3x3 or a 5x5 Gaussian filter with discrete values

Upvotes: 0

Views: 1062

Answers (1)

Raviteja Narra
Raviteja Narra

Reputation: 456

This implementation has a slight difference of, when normalization is applied, but it's just a minor thing.

def gaussian(sigma,Y,X):
    kernel = np.zeros((Y,X))
    ax = range(X) - np.floor(X/2)
    ay = range(Y) - np.floor(Y/2)
    xx,yy = np.meshgrid(ax,ay)
    kernel = np.exp(-0.5 * (np.square(xx) + np.square(yy)))/np.square(sigma)
    kernel = kernel/np.sum(kernel)
    return kernel 

print(np.sum(gaussian(0.3,5,5)))

Upvotes: 1

Related Questions