Laplacian sharpening¶
| Fact | Value |
|---|---|
| Also called | Laplacian sharpen |
| Status | Implemented and tested |
| Family | Linear spatial filters (also Enhancement, illumination and color) |
| Engine | Browser |
| Type | Linear, spatial, convolution |
| Output | Image |
| Operator shown as | dynamic-kernel (exact) |
| Support radius | 1 |
| Code | web/src/core/filters/linear/laplacian-sharpen.ts, laplacianSharpenFilter |
| Operator code | web/src/core/filters/linear/laplacian-sharpen.ts, laplacianSharpenKernel |
| Channels | RGB, Brightness only |
Overview¶
Laplacian sharpening subtracts a multiple of the Laplacian from the image. The Laplacian measures how much a pixel differs from its neighbors, so subtracting it makes each difference larger. Edges get steeper and fine detail stands out. It is the smallest sharpening filter: one 3x3 kernel.
How it works¶
- Compare each pixel with its neighbors. That is the Laplacian.
- Multiply the Laplacian by the amount.
- Subtract it from the pixel.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value
- \(L\): The 3x3 Laplacian kernel with a negative center, 4 or 8 neighbors
- \(\delta\): Unit impulse, 1 at the center and 0 elsewhere
- \(c\): Amount
- \(\star\): Correlation
L is the discrete Laplacian with a negative center. It is positive where a pixel is darker than its neighbors and negative where it is brighter. Subtracting c times L makes bright pixels brighter and dark pixels darker next to an edge, so the edge gets steeper. The whole filter is one kernel whose weights add up to 1.
The Laplacian with a negative center approximates \(\nabla^2 I\). Along a soft edge, \(\nabla^2 I\) is positive on the dark side and negative on the bright side. Subtracting it lowers the dark side and raises the bright side, so the edge gets steeper. Because the Laplacian kernel's weights add up to 0, the sharpening kernel's weights add up to 1, and flat areas keep their exact value.
Kernel or operator¶
The kernels for 4 and 8 neighbors with \(c = 1\), from the filter code.
Algorithm¶
- Build the kernel, 1 at the center minus c times the Laplacian kernel.
- Pad the image with the border mode and correlate it with the kernel.
- Clip to 0 to 255 and round to 8 bits. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Amount amount |
1 | 0.1 to 5 | The factor c that multiplies the Laplacian before it is subtracted. |
Neighbors neighbors |
4 neighbors | 4 neighbors, 8 neighbors | Which Laplacian kernel is used. |
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 |
RGB | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
laplacianSharpenKernel in web/src/core/filters/linear/laplacian-sharpen.ts builds the kernel from the Laplacian kernel in laplacian.ts, so both filters share one definition. The image is correlated with the kernel and the result is clipped.
Worked example¶
The center is 40 brighter than its neighbors. The Laplacian is 4 x 100 - 4 x 140 = -160, and subtracting it once raises the center to 300. The difference to the neighbors grows from 40 to 200, so the spot looks sharper.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels per channel. The kernel is always 3x3.
Artifacts and limitations¶
Limitations
- It works on the finest scale only. For wider detail, use the unsharp mask.
- It sharpens noise as strongly as edges.
Artifacts
- Thin halos along edges.
- Amplified noise and grain.
Tests¶
The test compares the filter with scipy.ndimage.correlate for both neighbor sets, two amounts and three border modes. The fixture script first checks the kernel against \(I - c\) times scipy.ndimage.laplace.
web/src/core/filters/filters.test.tsweb/src/core/filters/linear/linear5a.test.tsweb/src/core/filters/run.test.ts
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