Skip to content

Triangular filter

Fact Value
Also called Tent 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/triangular.ts, triangularFilter
Operator code web/src/core/filters/linear/triangular.ts, tentRow
Channels RGB, Brightness only, Color only

Overview

The triangular filter, also called the tent filter, blurs with weights that rise in equal steps to the center and fall again. It costs no more than a box filter of the same size, and it treats fine patterns better: they fade instead of coming back with reversed contrast.

Three panels: a zone plate, the same zone plate after a 9x9 box filter, and after a 9x9 tent filter. The box shows faint reversed rings at the outside. The tent fades them out. Three panels: a zone plate, the same zone plate after a 9x9 box filter, and after a 9x9 tent filter. The box shows faint reversed rings at the outside. The tent fades them out.
The tent filter fades the fine rings of a zone plate. The box filter of the same size brings some back reversed.

How it works

  1. Give the center pixel the largest weight, r + 1.
  2. Give each step away from the center one unit less, down to 1 at the edge of the window.
  3. Divide by the sum, (r + 1) squared.
  4. Average along every row, then along every column.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \sum_{j=-r}^{r} \sum_{i=-r}^{r} w_i \, w_j \, I(x+i,\, y+j), \qquad w_i = \frac{r + 1 - \lvert i \rvert}{(r+1)^2}, \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
  • \(w_i\): Normalized tent weight at offset i

The weights fall in a straight line from the center to the edge of the window and add up to 1. A triangle of width 2r + 1 is a box of width r + 1 convolved with itself, so the tent filter blurs like two box filters in a row. The 2D weight is the product of two 1D weights.

A box of width r + 1 convolved with itself is a triangle of width 2r + 1. So the tent filter equals two box filters in a row. The fixture script checks this identity before it writes the reference data.

In frequency, a box is a sinc, which has negative lobes. A product of two equal sincs is a squared sinc, which is never negative. That is why the tent filter does not reverse the contrast of fine patterns, while the box filter does.

Kernel or operator

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

\[ w = \frac{1}{9} \begin{bmatrix} 1 & 2 & 3 & 2 & 1 \end{bmatrix}^{\mathsf{T}} \qquad (n = 5,\ r = 2) \]
\[ T_5 = w\, w^{\mathsf{T}} = \frac{1}{81} \begin{bmatrix} 1 & 2 & 3 & 2 & 1 \\ 2 & 4 & 6 & 4 & 2 \\ 3 & 6 & 9 & 6 & 3 \\ 2 & 4 & 6 & 4 & 2 \\ 1 & 2 & 3 & 2 & 1 \end{bmatrix} \]

Algorithm

  1. Build the 1D weights r + 1 - |i| for i from -r to r and divide them by (r + 1) squared.
  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 31, odd px Width of the square window, 2r + 1.
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 r 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/9 2/9 3/9 2/9 1/9
Value x weight 4.44 8.89 13.33 47.78 23.89

The same edge row as for the binomial filter. The tent weights add up to 98.33 here. The filter code gives 98.33, which rounds to 98.

Complexity

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

N is the number of pixels per channel. The kernel is separable.

Artifacts and limitations

Limitations

  • The kernel has corners, so the blur is a little stronger along the diagonals than along the axes.
  • Edges blur as much as noise does.

Artifacts

  • Soft edges, as with every blur.

Tests

The test compares the filter with scipy.ndimage.correlate1d along both axes for sizes 3, 5 and 7 and all five border modes. The fixture script first checks the tent weights against the convolution of two boxes. The largest difference allowed is 1e-4 relative to the value.

References