Skip to content

Laplacian of Gaussian

Fact Value
Also called LoG, Mexican hat
Status Implemented and tested
Family Linear spatial filters (also Edge detection and boundary analysis)
Engine Browser
Type Linear, spatial, convolution
Output Image
Operator shown as dynamic-kernel (exact)
Support radius floor(4 sigma + 0.5)
Code web/src/core/filters/linear/dog-log.ts, logFilter
Operator code web/src/core/filters/linear/dog-log.ts, gaussianSecondDerivative
Channels Brightness only, RGB

Overview

The Laplacian of Gaussian, LoG, smooths the image with a Gaussian and takes the Laplacian, the sum of the second derivatives. Both steps form one kernel, often called the Mexican hat. Edges are where the result crosses zero, and blobs of a size close to sigma give strong peaks.

A plot of the 1D second derivative of a Gaussian with sigma 2, negative in the middle and slightly positive on both sides. A plot of the 1D second derivative of a Gaussian with sigma 2, negative in the middle and slightly positive on both sides.
The 1D second derivative of a Gaussian with sigma 2: negative in the middle, positive on both sides.

How it works

  1. Smooth the image with a Gaussian.
  2. Take the second derivative along x and along y.
  3. Add the two. Both steps run as one separable kernel per term.

Mathematical foundation

\[ \nabla^2 (G_{\sigma} * I) = \Bigl(\frac{\partial^2 G_{\sigma}}{\partial x^2} + \frac{\partial^2 G_{\sigma}}{\partial y^2}\Bigr) * I, \qquad \frac{d^2 g}{dx^2} = g(x)\Bigl(\frac{x^2}{\sigma^4} - \frac{1}{\sigma^2}\Bigr) \]

The formula shows the default parameter values.

where

  • \(G_{\sigma}\): The 2D Gaussian
  • \(g\): The 1D normalized Gaussian
  • \(\nabla^2\): The Laplacian, the sum of the second derivatives

Smoothing and the Laplacian are both linear, so they combine into one kernel. Each second derivative is separable: the second derivative of the 1D Gaussian g along one axis times g along the other. Here g is the normalized discrete Gaussian, as in scipy.ndimage.gaussian_laplace, truncated at 4 sigma. Edges are where the result crosses 0.

Because smoothing and differentiation are linear, the Laplacian of the smoothed image equals the image correlated with the Laplacian of the Gaussian. Each second derivative is separable, so the filter runs as two sums of two 1D passes. The kernel weights are the normalized Gaussian times (x^2 / sigma^4 - 1 / sigma^2), as in scipy.ndimage.gaussian_laplace, truncated at 4 sigma. The kernel adds up to nearly 0.

Kernel or operator

The Inspector shows the 2D kernel, built from the 1D kernels the filter runs. The Impulse response demo shows the Mexican hat.

Algorithm

  1. Pad the image with the border mode.
  2. Correlate with the second derivative kernel along x and the Gaussian along y.
  3. Correlate with the Gaussian along x and the second derivative along y.
  4. Add the two results and map the signed sum to 8 bits.

Parameters

Parameter Default Values Meaning
Sigma sigma 1.5 0.3 to 20 px The sigma of the Gaussian. The kernel reaches 4 sigma.
Output mapping outputMapping Offset by 128 Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 How signed results, such as gradients, become 8-bit pixel values.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope Brightness only RGB, Brightness only Which part of the color image the filter changes.

Implementation

logFilter in web/src/core/filters/linear/dog-log.ts correlates with the second derivative kernel along one axis and the Gaussian along the other, for both axes, and adds the results. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts, which handles the border mode, the channel scope, alpha and the output mapping. By default it works on luma and gives a gray result.

Worked example

x 28 30 31 32 33 35
Input 40 40 40 215 215 215
LoG + 128 133 148 138 118 108 123

Sigma 1.5 on the same step edge, with the offset mapping. The LoG is positive on the dark side and negative on the bright side, and it crosses 0 at the edge. The Marr-Hildreth detector marks such zero crossings. The center weight of the 1D second derivative kernel for sigma 2 is -0.04987.

Complexity

  • Time: \(O(N\,\sigma)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel. Four 1D passes of about 8 sigma taps.

Artifacts and limitations

Limitations

  • The result is signed and needs an output mapping or a zero-crossing step.
  • Corners are rounded off at large sigmas.

Artifacts

  • Double responses, a positive and a negative band, along each edge.

Tests

The reference is scipy.ndimage.gaussian_laplace. The test covers two sigmas for all five border modes, within the float tolerance.

References