Skip to content

Instantly share code, notes, and snippets.

@Seanny123
Created April 11, 2014 09:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seanny123/10452919 to your computer and use it in GitHub Desktop.
Save Seanny123/10452919 to your computer and use it in GitHub Desktop.
EE4208 plotting the Laplacian of Gaussian mask
import numpy as np
import math
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import sys
import pylab
from matplotlib.widgets import Slider
#from IPython.core import ultratb
#sys.excepthook = ultratb.FormattedTB(mode='Verbose',
# color_scheme='Linux', call_pdb=1)
range_inc = lambda start, end: range(start, end+1)
# Based off my own derivation and confirmed by http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm
def l_o_g(x, y, sigma):
# Formatted this way for readability
nom = ( (y**2)+(x**2)-2*(sigma**2) )
denom = ( (2*math.pi*(sigma**6) ))
expo = math.exp( -((x**2)+(y**2))/(2*(sigma**2)) )
return nom*expo/denom
# Create the laplacian of the gaussian, given a sigma
def create_log(sigma, size = 7):
w = math.ceil(float(size)*float(sigma))
# If the dimension is an even number, make it uneven
if(w%2 == 0):
print "even number detected, incrementing"
w = w + 1
# Now make the mask
l_o_g_mask = []
w_range = int(math.floor(w/2))
print "Going from " + str(-w_range) + " to " + str(w_range)
for i in range_inc(-w_range, w_range):
for j in range_inc(-w_range, w_range):
l_o_g_mask.append(l_o_g(i,j,sigma))
l_o_g_mask = np.array(l_o_g_mask)
l_o_g_mask = l_o_g_mask.reshape(w,w)
return l_o_g_mask
# Apply the l_o_g to the image
def run_l_o_g(sigma_val, size_val):
# Create the l_o_g mask
print "creating mask"
l_o_g_mask = create_log(sigma_val, size_val)
ha = hf.add_subplot(111, projection='3d')
X, Y = np.meshgrid(range(l_o_g_mask.shape[0]), range(l_o_g_mask.shape[1])) # `plot_surface` expects `x` and `y` data to be 2D
ha.plot_wireframe(X, Y, l_o_g_mask)
plt.show()
print 'done updating'
def update(val):
run_l_o_g(sigma.val, int(size.val))
print "update"
print "Start"
axcolor = 'lightgoldenrodyellow'
axsigma = pylab.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axsize = pylab.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
sigma = Slider(axsigma, 'Variance', 0.5, 3, valinit=1.4)
size = Slider(axsize, 'Size', 1, 10.0, valinit=5)
sigma_val = sigma.val
size_val = int(size.val)
sigma.on_changed(update)
size.on_changed(update)
hf = plt.figure()
run_l_o_g(sigma_val, size_val)
print "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment