Last active
February 6, 2024 11:19
-
-
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.
This file contains 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 | |
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