Weighted average filter¶
| Fact | Value |
|---|---|
| Also called | Custom kernel, Convolution with your own kernel |
| Status | Implemented and tested |
| Family | Linear spatial filters |
| Engine | Browser |
| Type | Linear, spatial, convolution |
| Output | Image |
| Operator shown as | dynamic-kernel (exact) |
| Support radius | half the larger side of the kernel |
| Code | web/src/core/filters/linear/weighted-average.ts, weightedAverageFilter |
| Operator code | web/src/core/filters/linear/weighted-average.ts, weightedKernel |
| Channels | RGB, Brightness only, Color only |
Overview¶
The weighted average filter runs a kernel that you type. Each pixel becomes the weighted sum of its neighbors, with your weights. It is the general linear filter: every blur, sharpening and edge kernel in a textbook can be tried here, up to 15x15.
How it works¶
- Type the weights, one row per line, or rows separated by semicolons.
- Lay the kernel over the image with its center on the pixel.
- Multiply each neighbor by the weight on top of it and add the results.
- With Normalize on, divide by the sum of the weights.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(I\): Input channel value
- \(I_{\text{out}}\): Output channel value before the output mapping
- \(K(i,j)\): The typed weight at offset (i, j) from the center cell
- \(s\): The divisor: the sum of the weights, or 1
The offsets i and j run over the kernel with its center cell at 0. Each output value is the weighted sum of the window under the kernel. Dividing by the sum s keeps flat areas at their value. The result can be negative for kernels with negative weights, and the output mapping decides how it is shown.
This is discrete correlation. Every linear, shift-invariant filter with a finite window has this form, so this one filter covers the whole linear family. The kernel is used as typed, not flipped. For a symmetric kernel, correlation and convolution give the same result. For other kernels, flip the kernel in both directions to get convolution.
When the weights add up to 1, flat areas keep their value. When they add up to 0, as in edge kernels, flat areas become 0 and the result is signed. Then turn Normalize off and pick an output mapping such as the absolute value.
Kernel or operator¶
The default kernel, from the filter code.
Algorithm¶
- Read the kernel text. If it cannot be read, use the default kernel and say why.
- With normalize on and a nonzero sum, divide every weight by the sum.
- Pad the image with the border mode and correlate it with the kernel, anchored at its center.
- Map the result to 8 bits with the output mapping. Keep alpha unchanged.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Kernel kernel |
1 2 1; 2 4 2; 1 2 1 | Rows of numbers, odd sides up to 15 | The weights, row by row. Separate rows with semicolons or new lines, and numbers with spaces or commas. Both sides must be odd, up to 15. |
Normalize normalize |
on | On or off | Divide the weights by their sum, so they add up to 1. Skipped when the sum is 0. |
Output mapping outputMapping |
Clip to 0 to 255 | Absolute value, Offset by 128, Stretch to full range, Clip to 0 to 255 | How signed results, such as gradients, become 8-bit pixel values. |
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. |
Implementation¶
parseKernel in web/src/core/filters/linear/weighted-average.ts reads the text. When it cannot, it says why:
- the kernel is empty,
- the rows have different lengths,
- a side is even or above 15,
- a value is not a number.
The Properties panel shows that message under the text box, and keeps the last good kernel until the text can be read. The image is correlated with the shared 2D code.
Worked example¶
Each value is multiplied by the weight on top of it: the center counts 4 times, the side neighbors twice and the corners once. The products add up to 800. Divided by the sum of the weights, 16, that gives 50.
Complexity¶
- Time: \(O(N\,k_w\,k_h)\)
- Memory: \(O(N)\)
N is the number of pixels per channel and k_w by k_h the kernel size.
Artifacts and limitations¶
Limitations
- The filter runs the kernel directly, so large kernels are slow. Separable ones are faster as their own filters.
- Correlation, not convolution. For a kernel that is not symmetric, flip it to get convolution.
Artifacts
- Depends on the kernel. Negative weights make halos, and unnormalized kernels change brightness.
Tests¶
The test compares the filter with scipy.ndimage.correlate for four kernels, including a 3x5 kernel and a kernel whose weights add up to 0, for three border modes. The fixture script checks the Sobel-shaped kernel against scipy.ndimage.sobel. Unit tests cover the kernel text rules and their messages.
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.correlate. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.correlate.html