Skip to content

Instantly share code, notes, and snippets.

@liob
Last active October 25, 2017 08:26
Show Gist options
  • Save liob/e784775e882b83749cb3bbcef480576e to your computer and use it in GitHub Desktop.
Save liob/e784775e882b83749cb3bbcef480576e to your computer and use it in GitHub Desktop.
create a n dimensional gaussian kernel for the given shape
import numpy as np
def gaussian(shape, sigma=1.0, mu=0.0):
""" create a n dimensional gaussian kernel for the given shape """
m = np.meshgrid(*[np.linspace(-1,1,s) for s in shape])
d = np.sqrt(np.sum([x*x for x in m], axis=0))
g = np.exp(-( (d-mu)**2 / ( 2.0 * sigma**2 ) ) )
return g / np.sum(g)
if __name__ == "__main__":
from matplotlib import pyplot as plt
fig, ax = plt.subplots(2)
ax[0].imshow(gaussian([20,10]))
ax[1].plot(gaussian([100]))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment