Skip to content

Laplacian

Fact Value
Also called Discrete Laplacian, Laplace operator
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 fixed-kernel (exact)
Support radius 1
Code web/src/core/filters/linear/laplacian.ts, laplacianFilter
Operator code web/src/core/filters/linear/laplacian.ts, laplacianKernel
Channels Brightness only, RGB

Overview

The Laplacian measures how much a pixel differs from the average of its neighbors. It is zero on flat areas and on straight ramps, and large at fine detail and edges. It is a second derivative, so it reacts strongly to noise.

Two line plots across a soft edge. The top shows the edge. The bottom shows the Laplacian, with a positive lobe on the dark side and a negative lobe on the bright side. Two line plots across a soft edge. The top shows the edge. The bottom shows the Laplacian, with a positive lobe on the dark side and a negative lobe on the bright side.
The Laplacian gives a negative and a positive lobe on the two sides of an edge.

How it works

  1. Look at the pixel and its 4 or 8 closest neighbors.
  2. Add up the neighbors.
  3. Subtract the center value, counted as many times as there are neighbors.
  4. The result is 0 where the pixel matches its surroundings.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \sum_{j=-1}^{1} \sum_{i=-1}^{1} L(i,j)\, I(x+i,\, y+j), \qquad \nabla^2 I = \frac{\partial^2 I}{\partial x^2} + \frac{\partial^2 I}{\partial y^2}, \qquad \text{neighbors} = \text{4} \]

The formula shows the default parameter values.

where

  • \(I\): Input values. Luma by default.
  • \(I_{\text{out}}\): Output value before the output mapping
  • \(L(i, j)\): Kernel weight at offset i to the right and j down
  • \(\nabla^2 I\): The Laplacian, the sum of the second derivatives along x and y

The kernel L is shown in the Operator tab. With 4 neighbors and a negative center, the result is the standard finite-difference approximation of the Laplacian: the sum of the second differences along x and along y. The weights add up to 0, so flat areas and straight ramps give 0. The positive-center kernel is the same kernel times -1.

Along one axis, the second difference I(x-1) - 2 I(x) + I(x+1) approximates the second derivative. The 4-neighbor kernel adds the second differences along x and y. The 8-neighbor kernel also adds the diagonal differences, which makes the response stronger and closer to the same in every direction. With the positive-center sign the kernel is multiplied by -1.

Kernel or operator

The two variants with the negative-center sign. The positive-center variants are the same kernels times -1.

\[ L_4 = \begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix}, \qquad L_8 = \begin{bmatrix} 1 & 1 & 1 \\ 1 & -8 & 1 \\ 1 & 1 & 1 \end{bmatrix} \]

Algorithm

  1. Convert to luma when the scope is brightness only.
  2. Pad the image with the border mode.
  3. Correlate with the selected 3x3 kernel.
  4. Map the signed values to 0 to 255 with the output mapping.

Parameters

Parameter Default Values Meaning
Neighbors neighbors 4 neighbors 4 neighbors, 8 neighbors Which neighbors the kernel compares with the center.
Sign sign Negative center (approximates the Laplacian) Negative center (approximates the Laplacian), Positive center (the same kernel times -1) Which sign convention the kernel uses.
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

laplacianKernel returns the selected kernel, and the filter correlates the image with it. By default the filter works on luma. The result is signed, so the output mapping decides how it is shown. Offset by 128 shows 0 as middle gray. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.

Worked example

\[ W = \begin{bmatrix} 10 & 10 & 10 \\ 10 & 60 & 10 \\ 10 & 10 & 10 \end{bmatrix}, \qquad L_4 \star W = 10 + 10 + 10 + 10 - 4 \cdot 60 = -200 \]

The center is brighter than its neighbors, so the negative-center Laplacian is negative. The output mapping turns -200 into a pixel value:

Mapping Pixel value
Absolute value 200
Offset by 128 0
Clip to 0 to 255 0

Complexity

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

N is the number of pixels per channel. The kernel has 5 or 9 weights.

Artifacts and limitations

Limitations

  • A second derivative amplifies noise strongly. Smooth first on noisy images, as the Laplacian of Gaussian does.
  • Gives a double response on each edge, one positive and one negative.
  • Gives no direction. Use Sobel when the direction matters.

Artifacts

  • Thin double lines along edges.
  • Speckled output on noisy images.

Tests

Both variants and both signs are compared with scipy.ndimage.correlate for three border modes, within 1e-4 relative to the value. The fixture generator first checks that the 4-neighbor kernel matches scipy.ndimage.laplace.

References