Skip to content

Binomial filter

Fact Value
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/binomial.ts, binomialFilter
Operator code web/src/core/filters/linear/binomial.ts, pascalRow
Channels RGB, Brightness only, Color only

Overview

The binomial filter blurs with weights taken from Pascal's triangle. The weights are whole numbers over a power of 2, so they are exact in binary arithmetic. For a size of 5 or more, the result is very close to a Gaussian blur.

A line plot across a sharp edge. The binomial filter of size 9 and the Gaussian filter with sigma 1.41 give curves that lie on top of each other. A line plot across a sharp edge. The binomial filter of size 9 and the Gaussian filter with sigma 1.41 give curves that lie on top of each other.
A binomial filter of size 9 and a Gaussian with sigma 1.41 blur an edge almost the same way.

How it works

  1. Take a row of Pascal's triangle, such as 1, 4, 6, 4, 1 for size 5.
  2. Divide it by its sum, 16, so the weights add up to 1.
  3. Average every row of the image with these weights.
  4. Average every column of that result with the same weights.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \sum_{j=0}^{n-1} \sum_{i=0}^{n-1} b_i \, b_j \, I(x+i-r,\, y+j-r), \qquad b_k = \frac{1}{2^{\,n-1}} \binom{n-1}{k}, \qquad r = \frac{n-1}{2}, \quad n = 5 \]

The formula shows the default parameter values.

where

  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value
  • \(n\): Window size in pixels, odd
  • \(r\): Window radius in pixels
  • \(b_k\): Normalized binomial weight at position k
  • \(\binom{n-1}{k}\): Binomial coefficient, entry k of row n - 1 of Pascal's triangle

The weights are a row of Pascal's triangle divided by its sum, 2 to the power n - 1. The 2D weight is the product of two 1D weights, so the filter runs as two 1D passes. The 1D weights are the probabilities of a fair coin giving k heads in n - 1 tosses, so they approach a Gaussian with variance (n - 1) / 4 as n grows.

The 1D weights are the binomial distribution with probability 1/2: the chance of k heads in n - 1 tosses of a fair coin. Its variance is (n - 1) / 4. By the central limit theorem the weights approach a Gaussian with that variance as n grows. So size 9 matches a Gaussian with sigma \(\sqrt{8/4} = \sqrt{2} \approx 1.41\), as the figure shows.

Row n - 1 of Pascal's triangle is also the result of convolving the pair \(\tfrac{1}{2}(1, 1)\) with itself n - 2 times. So the binomial filter equals averaging each pixel with its neighbor n - 1 times in a row. This is an exact identity, and a test checks it.

Kernel or operator

The 1D weights and the 2D kernel for size 5, from the filter code.

\[ b = \frac{1}{16} \begin{bmatrix} 1 & 4 & 6 & 4 & 1 \end{bmatrix}^{\mathsf{T}} \qquad (n = 5) \]
\[ B_5 = b\, b^{\mathsf{T}} = \frac{1}{256} \begin{bmatrix} 1 & 4 & 6 & 4 & 1 \\ 4 & 16 & 24 & 16 & 4 \\ 6 & 24 & 36 & 24 & 6 \\ 4 & 16 & 24 & 16 & 4 \\ 1 & 4 & 6 & 4 & 1 \end{bmatrix} \]

Algorithm

  1. Take row n - 1 of Pascal's triangle and divide it by 2 to the power n - 1.
  2. Pad each row with the border mode and filter it with these weights.
  3. Pad each column of that result and filter it with the same weights.
  4. Round to 8 bits. Keep alpha unchanged.

Parameters

Parameter Default Values Meaning
Size size 5 3 to 15, odd px Width of the square window. It is n, so the weights come from row n - 1 of Pascal's triangle.
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 filter runs as two 1D passes with the shared correlation code in web/src/core/filters/convolve.ts. On images with transparency, colors are averaged weighted by alpha. The studio splits the image into strips with a halo of (n - 1) / 2 pixels, so the strips give exactly the pixels of a whole-image run.

Worked example

Offset -2 -1 0 1 2
Value 40 40 40 215 215
Weight 1/16 4/16 6/16 4/16 1/16
Value x weight 2.50 10 15 53.75 13.44

The row crosses an edge from 40 to 215, and every row of the test image is the same. The products add up to 94.69. The filter code gives 94.69 for the pixel at offset 0, which rounds to 95.

Complexity

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

N is the number of pixels per channel. The kernel is separable, so the cost grows with n, not with n squared.

Artifacts and limitations

Limitations

  • The blur width is tied to the size. For a finer choice, use the Gaussian filter.
  • Large sizes give a large window for a modest blur, because the standard deviation is sqrt(n - 1) / 2.

Artifacts

  • Soft edges, as with every blur.

Tests

The test compares the filter with scipy.ndimage.correlate1d along both axes, with weights from scipy.special.comb, for sizes 3, 5 and 7 and all five border modes. Before writing the reference, the fixture script checks the weights against repeated averaging of two neighbors. The largest difference allowed is 1e-4 relative to the value.

References