Unsharp mask¶
| Fact | Value |
|---|---|
| Also called | Unsharp masking, 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 | floor(3 * sigma + 0.5) |
| Code | web/src/core/filters/linear/unsharp-mask.ts, unsharpMaskFilter |
| Operator code | web/src/core/filters/linear/unsharp-mask.ts, unsharpKernel |
| Channels | RGB, Brightness only |
Overview¶
Unsharp masking sharpens an image by adding back the detail that a blur removes. The name comes from darkroom work, where a blurred copy of a negative was used as a mask.
How it works¶
- Make a blurred copy of the image with a Gaussian blur.
- Subtract the copy from the image. What is left is the fine detail.
- Multiply the detail by the amount.
- Add it back to the image, except where the detail is below the threshold.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value
- \(G_{\sigma}\): Normalized Gaussian kernel with this sigma and radius floor(3 sigma + 0.5)
- \(\star\): Correlation. The Gaussian is symmetric, so it equals convolution.
- \(D\): Detail, the difference between the image and its blur
- \(a\): Amount
- \(t\): Threshold in 8-bit levels
The blur keeps the coarse picture. Subtracting it from the image leaves the detail D. Adding a times D back makes the detail stronger. Where the detail is smaller than the threshold, the pixel keeps its value. With threshold 0 the filter is linear, and the Operator tab shows its single kernel: (1 + a) at the center minus a times the Gaussian kernel.
With threshold 0, the result I + a (I - G * I) equals (1 + a) I - a (G * I). That is correlation with a single kernel, so the filter is linear and boosts high frequencies. A threshold above 0 makes the filter nonlinear, because the choice depends on the size of the detail.
Kernel or operator¶
The single equivalent kernel for sigma 0.6 and amount 1, from the filter code. The center weight is 1 + a minus a times the Gaussian center weight. The weights add up to 1.
Algorithm¶
- Blur the image with a Gaussian of the given sigma.
- Subtract the blur from the image to get the detail.
- Where the detail reaches the threshold, add amount times the detail to the image.
- Elsewhere keep the pixel.
- Clip to 0 to 255 and round to 8 bits. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Radius (sigma) sigma |
2 | 0.1 to 20 px | Sigma of the Gaussian blur that defines what counts as detail. |
Amount amount |
1 | 0 to 5 | How much of the detail is added back. |
Threshold threshold |
0 | 0 to 255 levels | Detail smaller than this, in 8-bit levels, is left alone. |
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¶
The blur uses the same Gaussian code as the Gaussian filter, with truncate 3. unsharpKernel builds the equivalent kernel for the Inspector. Results are clipped to 0 to 255, so strong settings can clip highlights and shadows. 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¶
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Value I | 40 | 40 | 40 | 70 | 150 | 200 | 215 | 215 | 215 |
| Blur G * I | 40.3 | 42.1 | 53.9 | 88 | 140.1 | 184.7 | 207.2 | 213.9 | 214.9 |
| Detail D | -0.3 | -2.1 | -13.9 | -18 | 9.9 | 15.3 | 7.8 | 1.1 | 0.1 |
| I + D | 39.7 | 37.9 | 26.1 | 52 | 159.9 | 215.3 | 222.8 | 216.1 | 215.1 |
| Pixel | 40 | 38 | 26 | 52 | 160 | 215 | 223 | 216 | 215 |
Sigma 1, amount 1, threshold 0, on a soft edge whose rows are all the same. The dark side dips below 40 and the bright side rises above 215. These are the overshoots that make the edge look sharper.
Complexity¶
- Time: \(O(N\,(2r+1))\)
- Memory: \(O(N)\)
N is the number of pixels per channel and r is the Gaussian radius. The blur is separable.
Artifacts and limitations¶
Limitations
- Cannot recover detail that the blur destroyed. It only boosts contrast at edges.
- Strong settings make bright and dark halos along edges.
- Without a threshold it also sharpens noise.
Artifacts
- Halos along high-contrast edges.
- Clipped highlights and shadows where the boost pushes values past 0 or 255.
Tests¶
The filter is compared with a NumPy version of the same formula built on scipy.ndimage.gaussian_filter, for two settings and three border modes, within 1e-4 relative to the value. The fixture generator first checks that formula against skimage.filters.unsharp_mask.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 3.
- scikit-image documentation, skimage.filters.unsharp_mask. https://scikit-image.org/docs/stable/api/skimage.filters.html#skimage.filters.unsharp_mask