Skip to content

Disk averaging filter

Fact Value
Also called Circular averaging filter, Defocus blur
Status Implemented and tested
Family Linear spatial filters
Engine Browser
Type Linear, spatial, convolution
Output Image
Operator shown as dynamic-kernel (exact)
Support radius ceil(radius - 0.5)
Code web/src/core/filters/linear/disk-average.ts, diskAverageFilter
Operator code web/src/core/filters/linear/disk-average.ts, diskKernel
Channels RGB, Brightness only, Color only

Overview

The disk averaging filter replaces each pixel with the plain average of a round window. A lens that is out of focus spreads every point of light into such a disk, so this filter is the standard model of defocus blur. It blurs the same amount in every direction, with a sharp outer edge.

Three panels: three small bright squares on black, the same after a disk blur of radius 5, and after a Gaussian blur with sigma 2.5. The disk makes round spots with a clear edge. The Gaussian makes soft blobs. Three panels: three small bright squares on black, the same after a disk blur of radius 5, and after a Gaussian blur with sigma 2.5. The disk makes round spots with a clear edge. The Gaussian makes soft blobs.
With Linear light on, a disk blur turns points of light into round spots. A Gaussian makes soft blobs.

How it works

  1. Draw a disk of the chosen radius around the pixel.
  2. Give each neighbor a weight equal to the part of its square that lies inside the disk.
  3. Divide by the area of the disk, so the weights add up to 1.
  4. Add up the weighted neighbors.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \sum_{j=-r}^{r} \sum_{i=-r}^{r} D(i,j)\, I(x+i,\, y+j), \qquad D(i,j) = \frac{\left| \mathcal{C}_R \cap \square_{i,j} \right|}{\pi R^2}, \qquad r = \lceil R - \tfrac{1}{2} \rceil, \quad R = 3 \]

The formula shows the default parameter values.

where

  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value
  • \(R\): Disk radius in pixels
  • \(r\): Half-size of the kernel in pixels
  • \(\mathcal{C}_R\): The disk of radius R around the center
  • \(\square_{i,j}\): The unit square of the kernel cell at offset (i, j)
  • \(\lvert \cdot \rvert\): Area

Each weight is the area of the disk of radius R that lies inside that pixel's square, divided by the area of the whole disk. So the weights add up to 1, and pixels on the edge of the disk count only for the part the disk covers. The areas are exact, from the integral of the circle's height.

The area of the disk inside a cell is an exact computation. Along x, the disk covers the heights from \(-h(x)\) to \(h(x)\), with \(h(x) = \sqrt{R^2 - x^2}\). The covered height inside the cell is \(\min(y_1, h) - \max(y_0, -h)\) where it is positive. Its integral has a closed form, because

\[ \int \sqrt{R^2 - x^2}\, dx = \tfrac{1}{2}\left(x \sqrt{R^2 - x^2} + R^2 \arcsin \tfrac{x}{R}\right) \]

where \(R\) is the radius and \(x\) the horizontal position. The code splits each cell at the points where \(h(x)\) crosses the cell's top or bottom, and integrates each piece exactly.

The Fourier transform of a disk is \(2 J_1(2\pi R f) / (2\pi R f)\), where \(J_1\) is the Bessel function of the first kind and \(f\) the frequency. It has zeros and negative lobes. So some fine patterns vanish completely and others come back with reversed contrast. This is also why defocus cannot be undone by a plain inverse filter.

Kernel or operator

The kernels for radius 1 and 2, from the filter code. The cells on the edge of the disk have partial weights.

\[ D_{R=1} = \begin{bmatrix} 0.0251 & 0.1453 & 0.0251 \\ 0.1453 & 0.3183 & 0.1453 \\ 0.0251 & 0.1453 & 0.0251 \end{bmatrix} \]
\[ D_{R=2} = \begin{bmatrix} 0 & 0.0170 & 0.0381 & 0.0170 & 0 \\ 0.0170 & 0.0784 & 0.0796 & 0.0784 & 0.0170 \\ 0.0381 & 0.0796 & 0.0796 & 0.0796 & 0.0381 \\ 0.0170 & 0.0784 & 0.0796 & 0.0784 & 0.0170 \\ 0 & 0.0170 & 0.0381 & 0.0170 & 0 \end{bmatrix} \]

Algorithm

  1. Compute the area of the disk inside each kernel cell, exactly.
  2. Divide every area by the sum, so the weights add up to 1.
  3. Pad the image with the border mode and correlate it with the kernel.
  4. Round to 8 bits. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Radius radius 3 0.5 to 25 px Radius of the disk in pixels. Partly covered pixels at its edge get partial weights.
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.
Linear light linearLight off On or off Blur linear light values instead of sRGB encoded values.

Implementation

The kernel is built once per radius by diskKernel in web/src/core/filters/linear/disk-average.ts, then the image is correlated with it by the shared 2D code. The disk is not separable, so the cost grows with the square of the radius. On images with transparency, colors are averaged weighted by alpha. For a realistic defocus, turn on Linear light: the filter then averages light, as a lens does.

Worked example

\[ D_{R=1} = \begin{bmatrix} 0.0251 & 0.1453 & 0.0251 \\ 0.1453 & 0.3183 & 0.1453 \\ 0.0251 & 0.1453 & 0.0251 \end{bmatrix} \]

For radius 1, the center cell lies fully inside the disk, so its area is 1. Each side cell holds the part of the disk beyond 0.5 pixels from the center, and each corner cell a small sliver. Divided by the disk area pi, the center weight is 0.3183, a side weight 0.1453 and a corner weight 0.0251. The weights add up to 1.

Complexity

  • Time: \(O(N\,(2r+1)^2)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel. The disk is not separable, so large radii are slow.

Artifacts and limitations

Limitations

  • Not separable, so the cost grows with the square of the radius.
  • Its frequency response has zeros and negative lobes, so some fine patterns reverse their contrast.

Artifacts

  • Ringing patterns on fine repeating detail, from the negative lobes.
  • Round bright spots around small highlights.

Tests

The reference kernel comes from numerical integration with scipy.integrate.quad, a different method from the closed form in the code. The fixture script checks it against point sampling with 400 by 400 points per cell before writing it. The test compares the kernels within 1e-10, and the filter with scipy.ndimage.correlate for three radii and all five border modes.

References