Skip to content

Edge enhance

Fact Value
Also called Edge enhancement, Pillow EDGE_ENHANCE
Status Implemented and tested
Family Artistic and geometric effects
Engine Browser
Type Linear, spatial, Laplacian sharpening with a fixed kernel
Output Image
Operator shown as fixed-kernel (exact)
Support radius 1
Code web/src/core/filters/artistic/kernels.ts, edgeEnhanceFilter
Operator code web/src/core/filters/artistic/kernels.ts, edgeEnhanceRows
Channels RGB, Brightness only

Overview

Edge enhance is one-click sharpening with Pillow's fixed kernels. It adds a Laplacian to the image, which makes edges steeper and leaves flat areas as they were.

Two panels: white disks on a dark background, and the same disks with crisper edges and a thin bright rim. Two panels: white disks on a dark background, and the same disks with crisper edges and a thin bright rim.
The test disks, and edge enhance with the default settings.

How it works

  1. Compare each pixel with its 8 neighbors.
  2. Push the pixel away from their mean by a fixed amount.
  3. Clip the result to 0 to 255.

Mathematical foundation

\[ \begin{gathered} K_{\text{normal}} = \frac{1}{2} \begin{bmatrix} -1 & -1 & -1 \\ -1 & 10 & -1 \\ -1 & -1 & -1 \end{bmatrix}, \quad K_{\text{more}} = \begin{bmatrix} -1 & -1 & -1 \\ -1 & 9 & -1 \\ -1 & -1 & -1 \end{bmatrix} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(K\): The 3 x 3 kernel

These are Pillow's EDGE_ENHANCE and EDGE_ENHANCE_MORE kernels. Both add up to 1, so flat areas keep their value. Each is the image plus a multiple of the 8-neighbor Laplacian with a positive center: half of it for normal, and all of it for more. That is Laplacian sharpening with a fixed amount.

Laplacian sharpening adds c times the Laplacian to the image. The normal kernel uses c = 1/2 and the more kernel c = 1, so a step from 50 to 200 overshoots more with the more kernel. Laplacian sharpening offers the same with an amount you can set.

Kernel or operator

The Inspector shows the chosen 3 x 3 kernel.

Algorithm

  1. Correlate the image with the chosen kernel.
  2. Clip to 0 to 255.

Parameters

Parameter Default Values Meaning
Strength strength Normal Normal, More Pillow's EDGE_ENHANCE or EDGE_ENHANCE_MORE kernel.
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.
Output mapping outputMapping Clip to 0 to 255 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.
Channels channelScope RGB RGB, Brightness only Which part of the color image the filter changes.

Implementation

edgeEnhanceFilter in web/src/core/filters/artistic/kernels.ts correlates the image with edgeEnhanceRows.

Worked example

Filter x = 3 x = 4 x = 5 x = 6 x = 7 x = 8
Input 50 50 50 200 200 200
Emboss 128 128 128 255 128 128
Outline 255 255 0 255 255 255
Edge enhance 50 50 0 255 200 200
Edge enhance, more 50 50 0 255 200 200

The middle row of a 12 by 3 image that steps from 50 to 200 between x = 5 and x = 6. Values are clipped to 0 to 255 and rounded. The emboss result depends on the pixel below and to the left, which is on the same side of a vertical step, so only the step itself shows.

Complexity

  • Time: \(O(N)\)
  • Memory: \(O(N)\)

N is the number of pixels.

Artifacts and limitations

Limitations

  • Fixed strength. Laplacian sharpening and unsharp masking offer control.
  • Boosts noise along with edges.

Artifacts

  • Bright and dark rims along strong edges.

Tests

The fixture cases compare every pixel with scipy.ndimage.correlate and the same kernels, for both strengths and three border modes. Before the cases are written, the results are checked against Pillow's EDGE_ENHANCE and EDGE_ENHANCE_MORE away from the edge.

References