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.
How it works¶
- Give the center pixel the largest weight, r + 1.
- Give each step away from the center one unit less, down to 1 at the edge of the window.
- Divide by the sum, (r + 1) squared.
- Average along every row, then along every column.
Mathematical foundation¶
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.
Algorithm¶
- Build the 1D weights r + 1 - |i| for i from -r to r and divide them by (r + 1) squared.
- Pad each row with the border mode and filter it with these weights.
- Pad each column of that result and filter it with the same weights.
- 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.
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 3.
- SciPy documentation, scipy.ndimage.correlate1d. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.correlate1d.html