[Math] Discrete Laplacian of Gaussian (LoG)

convolutionderivativesimage processinglaplacian

In Image processing, one often uses the discrete Laplacian of Gaussian (LoG) to do edge detection with

$LoG(x,y) = -\frac{1}{\pi \sigma^4}[1-\frac{x^2+y^2}{2\sigma^2}]\cdot e^{-\frac{x^2+y^2}{2\sigma^2}}$

Various sources here, here or here give discrete Kernels of the LoG to be convoluted with the input image to yield the filtered version. However, I do not understand the derivation of this Kernel from the function.

One possible Kernel for $\sigma = 1.4$ is

$K = \begin{bmatrix}0.0& 1.0& 1.0& 2.0& 2.0& 2.0& 1.0& 1.0& 0.0\\
1.0& 2.0& 4.0& 5.0& 5.0& 5.0& 4.0& 2.0& 1.0\\
1.0& 4.0& 5.0& 3.0& 0.0& 3.0& 5.0& 4.0& 1.0\\
2.0& 5.0& 3.0& -12.0& -24.0& -12.0& 3.0& 5.0& 2.0\\
2.0& 5.0& 0.0& -24.0& -40.0& -24.0& 0.0& 5.0& 2.0\\
2.0& 5.0& 3.0& -12.0& -24.0& -12.0& 3.0& 5.0& 2.0\\
1.0& 4.0& 5.0& 3.0& 0.0& 3.0& 5.0& 4.0& 1.0\\
1.0& 2.0& 4.0& 5.0& 5.0& 5.0& 4.0& 2.0& 1.0\\
0.0& 1.0& 1.0& 2.0& 2.0& 2.0& 1.0& 1.0& 0.0\end{bmatrix}$

My question is, how these discrete Kernels are derived from the $LoG(x,y)$ function. Is there any scaling factor involved? Is it an integration over the discrete pixel area?

My calculated values for the central point (-40.0) are:

$LoG(0,0) \approx -0.1624$

and

$\iint\limits_{-0.5}^{0.5}{LoG(x,y)} dx dy \approx -0.0761$

Best Answer

The answer is scaling. The matrix $K$ is simply a $n\times n$ matrix with $n$ being odd and

$K(i,j)=LoG(i-\frac{n-1}{2},j-\frac{n-1}{2})$.

The whole matrix can be scaled for easier calculation in the integer range.

The following python code produces a matrix, that is "close enough" to the wanted output:

import numpy as np

def LoG(sigma, x, y):
    laplace = -1/(np.pi*sigma**4)*(1-(x**2+y**2)/(2*sigma**2))*np.exp(-(x**2+y**2)/(2*sigma**2))
    return laplace

def LoG_discrete(sigma, n):
    l = np.zeros((n,n))
    for i in range(n):
        for j in range(n):
            l[i,j] = LoG(sigma, (i-(n-1)/2),(j-(n-1)/2))
    return l

sigma = 1.4

l = np.round(LoG_discrete(sigma, 9)*(-40/LoG(sigma,0,0)))

$K = \begin{bmatrix}0& 0& 1& 2& 2& 2& 1& 0& 0\\ 0& 1& 3& 5& 5& 5& 3& 1& 0\\ 1& 3& 5& 3& 0& 3& 5& 3& 1\\ 2& 5& 3&-12& -23& -12& 3& 5& 2\\ 2& 5& 0& -23& -40& -23& 0& 5& 2\\ 2& 5& 3&-12& -23& -12& 3& 5& 2\\ 1& 3& 5& 3& 0& 3& 5& 3& 1\\ 0& 1& 3& 5& 5& 5& 3& 1& 0\\ 0& 0& 1& 2& 2& 2& 1& 0& 0\end{bmatrix}$

Related Question