High-pass filter¶
| Fact | Value |
|---|---|
| Status | Implemented and tested |
| Family | Linear spatial filters |
| Engine | Browser |
| Type | Linear, spatial, convolution |
| Output | Image |
| Operator shown as | dynamic-kernel (exact) |
| Support radius | floor(3 * sigma + 0.5) |
| Code | web/src/core/filters/linear/high-pass.ts, highPassFilter |
| Operator code | web/src/core/filters/linear/high-pass.ts, boostKernel |
| Channels | Brightness only, RGB |
Overview¶
The high-pass filter keeps only the fine detail of an image. It subtracts a Gaussian blur from the image, so flat areas become 0 and edges remain. The result is signed, so the output mapping turns it into a picture. By default it adds 128, which makes flat areas middle gray.
How it works¶
- Make a blurred copy of the image.
- Subtract the copy from the image.
- Add 128, so the signed detail can be seen as lighter and darker gray.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(H\): High-pass result, signed
- \(G_{\sigma}\): Normalized Gaussian kernel with radius floor(3 sigma + 0.5)
- \(\star\): Correlation
- \(\sigma\): Standard deviation of the Gaussian in pixels
The Gaussian blur keeps the coarse picture. What is left after subtracting it from the image is the fine detail H, a signed value that is 0 in flat areas. The output mapping turns it into a visible image. The default adds 128, so flat areas are middle gray.
The filter is one kernel: a unit impulse minus the Gaussian kernel. Its weights add up to 0, so flat areas give exactly 0. The Gaussian passes low frequencies, so the difference passes the high ones. Its frequency response is close to \(1 - e^{-2\pi^2 \sigma^2 f^2}\) for the frequency \(f\) in cycles per pixel. That is an approximation, because the Gaussian kernel is sampled and cut off at 3 sigma.
Kernel or operator¶
The kernel for \(\sigma = 0.5\), from the filter code. The weights add up to 0.
Algorithm¶
- Blur the image with a Gaussian of the given sigma.
- Subtract the blur from the image.
- Map the signed result to 8 bits with the output mapping. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Sigma sigma |
2 | 0.3 to 50 px | Standard deviation of the Gaussian low-pass that is subtracted. |
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¶
boostPlane in web/src/core/filters/linear/high-pass.ts blurs with the Gaussian filter code and subtracts. The high-boost filter shares this function. The filter works on luma by default, so the result is gray.
Worked example¶
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Value I | 40 | 40 | 40 | 70 | 150 | 200 | 215 | 215 | 215 |
| I - G * I | -0.3 | -2.1 | -13.9 | -18 | 9.9 | 15.3 | 7.8 | 1.1 | 0.1 |
| Offset by 128 | 128 | 126 | 114 | 110 | 138 | 143 | 136 | 129 | 128 |
Sigma 1 on a soft edge whose rows are all the same. Far from the edge the detail is 0, so the pixel is 128. The dark side of the edge is negative and the bright side positive.
Complexity¶
- Time: \(O(N\,(2r+1))\)
- Memory: \(O(N)\)
N is the number of pixels per channel and r the Gaussian radius. The blur is separable.
Artifacts and limitations¶
Limitations
- The result is not a photo. It is the detail around middle gray.
- Noise is detail too, and it stays.
Artifacts
- Halos around large edges, with the width of the blur.
Tests¶
The test compares the signed result before the output mapping with the image minus scipy.ndimage.gaussian_filter, for two sigmas and three border modes.
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.gaussian_filter. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_filter.html