Skip to content

Box filter

Fact Value
Also called Mean filter, Average filter, Per-channel box filter
Status Implemented and tested
Family Linear spatial filters
Engine Browser
Type Linear, spatial, separable convolution
Output Image
Operator shown as dynamic-kernel (exact)
Support radius (size - 1) / 2
Code web/src/core/filters/linear/box.ts, boxFilter
Operator code web/src/core/filters/linear/box.ts, boxKernel
Channels RGB, Brightness only, Color only

Overview

The box filter replaces each pixel with the plain average of a square window. It is the simplest blur, and with running sums also the fastest. It is a good baseline to compare other blurs with.

Three panels: a zone plate, the same zone plate after a 9x9 box filter, and after a Gaussian blur with sigma 2. Three panels: a zone plate, the same zone plate after a 9x9 box filter, and after a Gaussian blur with sigma 2.
A 9x9 box filter turns some fine rings of a zone plate into rings of reversed contrast. A Gaussian blur fades them out instead.

How it works

  1. Take the square window around the pixel. Its width is the window size.
  2. Add up every value in the window.
  3. Divide by the number of pixels in the window.
  4. Write the result as the new pixel value. Do this for every pixel and channel.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \frac{1}{n^2} \sum_{j=-r}^{r} \sum_{i=-r}^{r} I(x+i,\, y+j), \qquad n = 3, \quad r = \frac{n-1}{2} \]

The formula shows the default parameter values.

where

  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value
  • \((x, y)\): Pixel position. x grows to the right and y grows downward.
  • \(n\): Window width in pixels. It is odd.
  • \(r\): Window radius in pixels

Each output pixel is the mean of the n by n window around it. Every weight is 1 / n^2, so the weights add up to 1. The kernel is the outer product of two 1D means, so the filter runs as a mean along rows and then a mean along columns.

All weights are equal, so the box filter is a moving average. Its frequency response is a product of two sinc-shaped curves. They have negative side lobes, which is why some fine patterns come back with reversed contrast, as in the figure.

Kernel or operator

The kernel for a 3x3 window. Larger windows have more cells with the same weight, 1 divided by the number of cells.

\[ B_3 = \frac{1}{9} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix} \]

Algorithm

  1. Pad each row with the border mode and add up its first n values.
  2. Slide along the row. Add the value that enters the window and subtract the value that leaves it.
  3. Divide each sum by n to get the row means.
  4. Do the same along each column of the row means.
  5. Round to 8 bits. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 51, odd px Width and height of the square window.
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

Each pass keeps a running sum over the window. Sliding the window by one pixel adds the entering value and subtracts the leaving value, so the cost per pixel does not depend on the window size. On images with transparency, colors are averaged weighted by alpha. 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

\[ W = \begin{bmatrix} 30 & 40 & 50 \\ 40 & 90 & 60 \\ 50 & 60 & 70 \end{bmatrix}, \qquad \frac{1}{9} \sum W = \frac{490}{9} = 54.44 \;\rightarrow\; 54 \]

The 3x3 window around the center pixel (value 90) adds up to 490. The mean is 54.44, which rounds to 54.

Complexity

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

N is the number of pixels per channel. Running sums make the cost per pixel independent of the window size. Adding up every window directly would cost O(N\,n^2).

Artifacts and limitations

Limitations

  • Every neighbor counts the same, so the result looks blockier than a Gaussian blur.
  • Its frequency response has negative side lobes. Some fine stripes come back with reversed contrast.
  • Edges blur as much as noise does.

Artifacts

  • A bright point spreads into a square.
  • Faint rings with reversed contrast in fine repeating patterns.

Tests

The test compares the filter with scipy.ndimage.uniform_filter for window sizes 3 and 5 and all five border modes. The largest difference allowed is 1e-4 relative to the value. Other tests check that any region gives the same pixels as the whole image, and that alpha stays unchanged.

References