Box blur explained
A box blur (also known as a box linear filter) is a spatial domain linear filter in which each pixel in the resulting image has a value equal to the average value of its neighboring pixels in the input image. It is a form of low-pass ("blurring") filter. A 3 by 3 box blur ("radius 1") can be written as matrix
\begin{bmatrix}1&1&1\ 1&1&1\ 1&1&1\end{bmatrix}.
Due to its property of using equal weights, it can be implemented using a much simpler accumulation algorithm, which is significantly faster than using a sliding-window algorithm.[1]
Box blurs are frequently used to approximate a Gaussian blur.[2] By the central limit theorem, repeated application of a box blur will approximate a Gaussian blur.[3]
In the frequency domain, a box blur has zeros and negative components. That is, a sine wave with a period equal to the size of the box will be blurred away entirely, and wavelengths shorter than the size of the box may be phase-reversed, as seen when two bokeh circles touch to form a bright spot where there would be a dark spot between two bright spots in the original image.
Extensions
- Gwosdek, et al. has extended Box blur to take a fractional radius: the edges of the 1-D filter are expanded with a fraction. It makes slightly better gaussian approximation possible due to the elimination of integer-rounding error.[3]
- Mario Klingemann has a "stack blur" that tries to better emulate gaussian's look in one pass by stacking weights:
\begin{bmatrix}
1&2&3&2&1
\end{bmatrix}
[4] [5] The triangular impulse response it forms decomposes to two rounds of box blur.
[3] - Stacked Integral Image by Bhatia et al. takes the weighted average of a few box blurs to fit the gaussian response curve.[3]
Implementation
The following pseudocode implements a 3x3 box blur.Box blur (image)
The example does not handle the edges of the image, which would not fit inside the kernel, so that these areas remain unblurred. In practice, the issue is better handled by:[3]
- Introducing an alpha channel to represent the absence of colors;
- Extending the boundary by filling in values, ranked by quality:
- Fill in a mirrored image at the border
- Fill in a constant color extending from the last pixel
- Pad in a fixed color
A number of optimizations can be applied when implementing the box blur of a radius r and N pixels:[6]
- The box blur is a separable filter, so that only two 1D passes of averaging pixels will be needed, one horizontal and one vertical, for each pixel. This lowers the complexity from to . In digital signal processing terminology, each pass is a moving-average filter.
- Accumulation. Instead of discarding the sum for each pixel, the algorithm re-uses the previous sum, and updates it by subtracting away the old pixel and adding the new pixel in the blurring range. A summed-area table can be used similarly. This lowers the complexity from to .
- When being used in multiple passes to approximate a Gaussian blur, the cascaded integrator–comb filter construction allows for doing the equivalent operation in a single pass.[7]
See also
Notes and References
- Wojciech Jarosz. 2001. Fast Image Convolutions.
- W3C SVG1.1 specification, 15.17 Filter primitive 'feGaussianBlur'.
- Getreuer . Pascal . ASurvey of Gaussian Convolution Algorithms . Image Processing on Line . 17 December 2013 . 3 . 286–310 . 10.5201/ipol.2013.87. free . (code doc)
- Web site: Stackblur and Quadratic Stackblur . observablehq.com . en . 12 November 2018.
- Web site: How to Blur an Image on Android . Medium . en . 10 February 2020.
- Web site: Kutsvir . Ivan . Fastest Gaussian Blur (in linear time) . 4 April 2020.
- Web site: Sitaker . Kragen . Hmm, aside from my note about how the family of kernels Costella discovered are precisely the uniform cardinal B-splines... . Hacker News.