General Laplacian of Gaussian kernel

3.6k Views Asked by At

I am having difficulty implementing a Laplacian of Gaussian kernel. I have the following code and I am trying to implement a 9x9 kernel with sigma = 1.4. The kernel is shown on this link

http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm

However, my values are nothing like those in that kernel, I think my function is off. Help would be greatly appreciated. Thank you.

import math
pi= math.pi
log = [[0 for x in range(9)] for x in range(9)]
def genlog(log,size,o):
    for i in range(-size/2,size/2):
        for j in range(-size/2,size/2):
            log[i][j] = -(pi*o**4)**(-1)*(1-(i**2+j**2)/(2*o**2))*math.exp(-(i**2+j**2)/(2*o**2))
def printlog(log,size):
    for i in range(-size/2,size/2):
        print ' '.join(str(log[i][j]) for j in range(-size/2,size/2))

genlog(log,9,1.4)
printlog(log,9)
2

There are 2 best solutions below

2
On

the question is neraly three years old, so i don't know if it's useful, but i implemented the cited algorithm in excel and found that your function should be writed as such :

log[i][j] = (-1)/(pi*o**4)*(1-(i**2+j**2)/(2*o**2))*math.exp(-(i**2+j**2)/(2*o**2))

A scaling factor should be used, too, to account for the source image size.

5
On

Your code is pretty hard to read to begin with, but for starters, you have:

log[i][j] = (pi*o**4)**.5*...

When it should really be, according to your formula:

log[i][j] = -(pi*o**4)**(-1)*...

Raising something to the .5 is actually a square root. What you're looking for is the inverse, raising it by -1. Also, you forgot to make it negative.