Skip to content

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.

A line plot across a soft edge, before and after unsharp masking with sigma 2 and amount 1. The result dips below the dark level and rises above the bright level next to the edge. A line plot across a soft edge, before and after unsharp masking with sigma 2 and amount 1. The result dips below the dark level and rises above the bright level next to the edge.
Unsharp masking steepens an edge and adds a dark and a bright overshoot.

How it works

  1. Make a blurred copy of the image with a Gaussian blur.
  2. Subtract the copy from the image. What is left is the fine detail.
  3. Multiply the detail by the amount.
  4. Add it back to the image, except where the detail is below the threshold.

Mathematical foundation

\[ \begin{gathered} D(x,y) = I(x,y) - (G_{\sigma} \star I)(x,y), \\[4pt] I_{\text{out}}(x,y) = \begin{cases} I(x,y) + a\, D(x,y) & \text{if } \lvert D(x,y) \rvert \ge t \\ I(x,y) & \text{otherwise} \end{cases} \\[4pt] \sigma = 2, \quad a = 1, \quad t = 0 \end{gathered} \]

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.

\[ U_{\sigma=0.6,\ a=1} = \begin{bmatrix} 0.0000 & -0.0004 & -0.0017 & -0.0004 & 0.0000 \\ -0.0004 & -0.0274 & -0.1099 & -0.0274 & -0.0004 \\ -0.0017 & -0.1099 & 1.5593 & -0.1099 & -0.0017 \\ -0.0004 & -0.0274 & -0.1099 & -0.0274 & -0.0004 \\ 0.0000 & -0.0004 & -0.0017 & -0.0004 & 0.0000 \end{bmatrix} \]

Algorithm

  1. Blur the image with a Gaussian of the given sigma.
  2. Subtract the blur from the image to get the detail.
  3. Where the detail reaches the threshold, add amount times the detail to the image.
  4. Elsewhere keep the pixel.
  5. 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