Last active
October 25, 2017 08:26
-
-
Save liob/e784775e882b83749cb3bbcef480576e to your computer and use it in GitHub Desktop.
create a n dimensional gaussian kernel for the given shape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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