Skip to content

Weighted average filter

Fact Value
Also called Custom kernel, Convolution with your own kernel
Status Implemented and tested
Family Linear spatial filters
Engine Browser
Type Linear, spatial, convolution
Output Image
Operator shown as dynamic-kernel (exact)
Support radius half the larger side of the kernel
Code web/src/core/filters/linear/weighted-average.ts, weightedAverageFilter
Operator code web/src/core/filters/linear/weighted-average.ts, weightedKernel
Channels RGB, Brightness only, Color only

Overview

The weighted average filter runs a kernel that you type. Each pixel becomes the weighted sum of its neighbors, with your weights. It is the general linear filter: every blur, sharpening and edge kernel in a textbook can be tried here, up to 15x15.

Three panels: a Siemens star, the star after the blur kernel 1 2 1; 2 4 2; 1 2 1, and after the vertical edge kernel -1 0 1; -2 0 2; -1 0 1 with the absolute value. The blur softens the spokes. The edge kernel shows white lines at the spoke edges. Three panels: a Siemens star, the star after the blur kernel 1 2 1; 2 4 2; 1 2 1, and after the vertical edge kernel -1 0 1; -2 0 2; -1 0 1 with the absolute value. The blur softens the spokes. The edge kernel shows white lines at the spoke edges.
The same image through a blur kernel and through an edge kernel.

How it works

  1. Type the weights, one row per line, or rows separated by semicolons.
  2. Lay the kernel over the image with its center on the pixel.
  3. Multiply each neighbor by the weight on top of it and add the results.
  4. With Normalize on, divide by the sum of the weights.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \frac{1}{s} \sum_{j} \sum_{i} K(i,j)\, I(x+i,\, y+j), \qquad s = \begin{cases} \sum_{j} \sum_{i} K(i,j) & \text{if normalize is on and the sum is not } 0 \\ 1 & \text{otherwise} \end{cases} \]

The formula shows the default parameter values.

where

  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value before the output mapping
  • \(K(i,j)\): The typed weight at offset (i, j) from the center cell
  • \(s\): The divisor: the sum of the weights, or 1

The offsets i and j run over the kernel with its center cell at 0. Each output value is the weighted sum of the window under the kernel. Dividing by the sum s keeps flat areas at their value. The result can be negative for kernels with negative weights, and the output mapping decides how it is shown.

This is discrete correlation. Every linear, shift-invariant filter with a finite window has this form, so this one filter covers the whole linear family. The kernel is used as typed, not flipped. For a symmetric kernel, correlation and convolution give the same result. For other kernels, flip the kernel in both directions to get convolution.

When the weights add up to 1, flat areas keep their value. When they add up to 0, as in edge kernels, flat areas become 0 and the result is signed. Then turn Normalize off and pick an output mapping such as the absolute value.

Kernel or operator

The default kernel, from the filter code.

\[ K = \frac{1}{16} \begin{bmatrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \end{bmatrix} \qquad \text{(the default kernel, normalized)} \]

Algorithm

  1. Read the kernel text. If it cannot be read, use the default kernel and say why.
  2. With normalize on and a nonzero sum, divide every weight by the sum.
  3. Pad the image with the border mode and correlate it with the kernel, anchored at its center.
  4. Map the result to 8 bits with the output mapping. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Kernel kernel 1 2 1; 2 4 2; 1 2 1 Rows of numbers, odd sides up to 15 The weights, row by row. Separate rows with semicolons or new lines, and numbers with spaces or commas. Both sides must be odd, up to 15.
Normalize normalize on On or off Divide the weights by their sum, so they add up to 1. Skipped when the sum is 0.
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.
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, Color only Which part of the color image the filter changes.

Implementation

parseKernel in web/src/core/filters/linear/weighted-average.ts reads the text. When it cannot, it says why:

  • the kernel is empty,
  • the rows have different lengths,
  • a side is even or above 15,
  • a value is not a number.

The Properties panel shows that message under the text box, and keeps the last good kernel until the text can be read. The image is correlated with the shared 2D code.

Worked example

\[ W = \begin{bmatrix} 10 & 20 & 30 \\ 40 & 50 & 60 \\ 70 & 80 & 90 \end{bmatrix}, \qquad \frac{1}{16} \sum K \odot W = \frac{800}{16} = 50 \]

Each value is multiplied by the weight on top of it: the center counts 4 times, the side neighbors twice and the corners once. The products add up to 800. Divided by the sum of the weights, 16, that gives 50.

Complexity

  • Time: \(O(N\,k_w\,k_h)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and k_w by k_h the kernel size.

Artifacts and limitations

Limitations

  • The filter runs the kernel directly, so large kernels are slow. Separable ones are faster as their own filters.
  • Correlation, not convolution. For a kernel that is not symmetric, flip it to get convolution.

Artifacts

  • Depends on the kernel. Negative weights make halos, and unnormalized kernels change brightness.

Tests

The test compares the filter with scipy.ndimage.correlate for four kernels, including a 3x5 kernel and a kernel whose weights add up to 0, for three border modes. The fixture script checks the Sobel-shaped kernel against scipy.ndimage.sobel. Unit tests cover the kernel text rules and their messages.

References