Skip to content

Instantly share code, notes, and snippets.

@realamirhe
Last active July 12, 2020 04:52
Show Gist options
  • Save realamirhe/6614d7a532d6fc65afd8dcd6056d58ea to your computer and use it in GitHub Desktop.
Save realamirhe/6614d7a532d6fc65afd8dcd6056d58ea to your computer and use it in GitHub Desktop.
Difference of Gaussian Filter with numpy
# http://fourier.eng.hmc.edu/e161/lectures/gradient/node9.html
"""
σ1 := width or std for first gussian distribution
σ2 := width or std for second gussian distribution
"""
def DoG(σ1, σ2, shape):
if (σ1 == 0 or σ2 == 0):
print("WARNING: Result Filter gonna be all zero. σ1 and σ2 must be non zero")
return np.zeros(shape)
def _(x, y):
xy_exp = -0.5 * ((x-(shape[0]//2))**2 + (y-(shape[1]//2))**2)
return (np.exp(xy_exp/σ1**2)/σ1 - np.exp(xy_exp/σ2**2)/σ2) / np.sqrt(2 * np.pi)
return np.fromfunction(_, shape)
"""
mechanistic-v1-model or gabor filter
λ := wavelength
θ := orientation
σ := standard deviation
γ := aspect ratio
np.meshgrid is tiny faster than our Gabor caused by alignment on L:32
https://en.wikipedia.org/wiki/Gabor_filter
"""
def Gabor(λ, θ, σ, γ, shape):
if (λ == 0 or σ == 0):
print("WARNING: Result Filter gonna be all zero. λ and σ must be non zero")
return np.zeros(shape)
def _(x, y):
_x, _y = x-(shape[0]//2), y-(shape[1]//2)
X = _x*np.cos(θ) + _y*np.sin(θ)
λ2Y2 = λ**2 * (-_x*np.sin(θ) + _y*np.cos(θ))**2
return np.exp(-0.5*(X**2 + λ2Y2)/σ**2) * np.cos(2*np.pi*X/λ)
return np.fromfunction(_, shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment