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
Hi I think the problem is that for a gaussian filter the normalization factor depends on how many dimensions you used.
So the filter looks like this
What you miss is the square of the normalization factor! And need to renormalize the whole matrix because of computing accuracy!
The code is attached here:
def gaussian_filter(shape =(5,5), sigma=1):
x, y = [edge /2 for edge in shape]
grid = np.array([[((i**2+j**2)/(2.0*sigma**2)) for i in xrange(-x, x+1)] for j in xrange(-y, y+1)])
g_filter = np.exp(-grid)/(2*np.pi*sigma**2)
g_filter /= np.sum(g_filter)
return g_filter
print gaussian_filter()
The output without normalized to sum of 1:
[[ 0.00291502 0.01306423 0.02153928 0.01306423 0.00291502]
[ 0.01306423 0.05854983 0.09653235 0.05854983 0.01306423]
[ 0.02153928 0.09653235 0.15915494 0.09653235 0.02153928]
[ 0.01306423 0.05854983 0.09653235 0.05854983 0.01306423]
[ 0.00291502 0.01306423 0.02153928 0.01306423 0.00291502]]
The output divided by np.sum(g_filter):
[[ 0.00296902 0.01330621 0.02193823 0.01330621 0.00296902]
[ 0.01330621 0.0596343 0.09832033 0.0596343 0.01330621]
[ 0.02193823 0.09832033 0.16210282 0.09832033 0.02193823]
[ 0.01330621 0.0596343 0.09832033 0.0596343 0.01330621]
[ 0.00296902 0.01330621 0.02193823 0.01330621 0.00296902]]