1

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)
hippietrail
  • 15,848
  • 18
  • 99
  • 158
NamPNQ
  • 379
  • 4
  • 13
  • 2
    I don't know. What *is* wrong with your code? In what way is it not working? (As an aside, you probably want to be doing this with [Numpy](http://numpy.scipy.org/).) – Iguananaut Nov 26 '12 at 20:16
  • Also, this doesn't really answer your question, because it may be that you're working on implementing this as an exercise which is fine. But if it's for production use you may want to consider the implementation of this that's already in scipy: http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.filters.gaussian_laplace.html#scipy.ndimage.filters.gaussian_laplace – Iguananaut Nov 26 '12 at 20:27
  • i need log kernel, not need build-in function – NamPNQ Nov 26 '12 at 20:39
  • Possible duplicate of [Laplacian of Gaussian](https://stackoverflow.com/questions/2556958/laplacian-of-gaussian) – Cris Luengo Sep 11 '19 at 13:25
  • I had found the correct answer in math forum: https://math.stackexchange.com/questions/2445994/discrete-laplacian-of-gaussian-log – NamPNQ Mar 25 '21 at 12:38

2 Answers2

2

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.

Laurent Jégou
  • 1,582
  • 1
  • 14
  • 16
1

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.

jdotjdot
  • 16,134
  • 13
  • 66
  • 118