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.
How it works¶
- Draw a disk of the chosen radius around the pixel.
- Give each neighbor a weight equal to the part of its square that lies inside the disk.
- Divide by the area of the disk, so the weights add up to 1.
- Add up the weighted neighbors.
Mathematical foundation¶
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
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.
Algorithm¶
- Compute the area of the disk inside each kernel cell, exactly.
- Divide every area by the sum, so the weights add up to 1.
- Pad the image with the border mode and correlate it with the kernel.
- 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¶
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.
web/src/core/filters/filters.test.tsweb/src/core/filters/linear/linear5a.test.tsweb/src/core/filters/run.test.ts
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5.
- MathWorks documentation, fspecial with the disk type. https://www.mathworks.com/help/images/ref/fspecial.html
- SciPy documentation, scipy.integrate.quad, used for the reference kernel. https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html