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.
How it works¶
- Look at the pixel and its 4 or 8 closest neighbors.
- Add up the neighbors.
- Subtract the center value, counted as many times as there are neighbors.
- The result is 0 where the pixel matches its surroundings.
Mathematical foundation¶
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.
Algorithm¶
- Convert to luma when the scope is brightness only.
- Pad the image with the border mode.
- Correlate with the selected 3x3 kernel.
- 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¶
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¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 3.
- SciPy documentation, scipy.ndimage.laplace. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.laplace.html
- OpenCV documentation, image filtering, Laplacian. https://docs.opencv.org/4.x/d4/d86/group__imgproc__filter.html