In [3]:
import numpy as np
def log(s, mu, sigma):
mu = np.array(mu)
s = np.array(s)
if np.product(np.shape(s)) == 1:
x = np.arange(s)
coord_center = (x-mu)**2
else:
(rr, cc) = np.indices( s)
coord_center = (rr-mu[0])**2 + (cc-mu[1])**2
gauss_factor = coord_center/(2.*sigma**2)
gauss_factor_r = np.ravel(gauss_factor)
exp_factor = np.exp(-gauss_factor_r)
exp_factor = exp_factor.reshape( np.shape(coord_center))
g = -(((1 - gauss_factor )/ (sigma**4 * np.pi)) * exp_factor)
return g
In [1]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python log.ipynb
import sys
import os
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
if testing:
s, mu, sigma = 5, 3, 0.8
F = ia.log(s, mu, sigma)
print('image dimensions = ', s)
print('center of function = ', mu)
print('spread factor =', sigma)
print('Laplacian of Gaussian image : \n', F.round(2))
In [3]:
if testing:
s, mu, sigma = 256, 128, 8
F = ia.log(s, mu, sigma)
print('image dimensions = ', s)
print('center of function = ', mu)
print('spread factor =', sigma)
plt.plot(F)
plt.title('Laplacian of Gaussian')
In [4]:
if testing:
s, mu, sigma = [5, 7], [3, 4], .5
F = ia.log(s, mu, sigma)
print('image dimensions = ', s)
print('center of function = ', mu)
print('spread factor =', sigma)
print('Laplacian of Gaussian image : \n', F.round(2))
In [5]:
if testing:
s, mu, sigma = [128, 128], [64, 64], 4
F = ia.log(s, mu, sigma)
print('image dimensions = ', s)
print('center of function = ', mu)
print('spread factor =', sigma)
ia.adshow(ia.normalize(F), 'Laplacian of Gaussian')
In [6]:
if testing:
s, mu, sigma = [256, 256], [128, 128], 20
F = ia.log(s, mu, sigma)
print('image dimensions = ', s)
print('center of function = ', mu)
print('spread factor =', sigma)
ia.adshow(ia.normalize(F), 'Laplacian of Gaussian')
In [7]:
if testing:
s, mu, sigma = [256, 256], [128, 128], 20
print('Computational time is:')
%timeit ia.log(s, mu, sigma)
In [ ]: