Skip to content

Instantly share code, notes, and snippets.

@thomasaarholt
Last active February 6, 2024 11:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasaarholt/267ec4fff40ca9dff1106490ea3b7567 to your computer and use it in GitHub Desktop.
Save thomasaarholt/267ec4fff40ca9dff1106490ea3b7567 to your computer and use it in GitHub Desktop.
Fastest found numpy method of generating a 2D gaussian kernel of size n x n and standard deviation std.
import numpy as np
from scipy import signal
def gaussian_kernel(n, std, normalised=False):
'''
Generates a n x n matrix with a centered gaussian
of standard deviation std centered on it. If normalised,
its volume equals 1.'''
gaussian1D = signal.gaussian(n, std)
gaussian2D = np.outer(gaussian1D, gaussian1D)
if normalised:
gaussian2D /= (2*np.pi*(std**2))
return gaussian2D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment