Skip to content

Kuwahara filter

Fact Value
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, spatial, adaptive local mean
Output Image
Operator shown as algorithm (exact)
Support radius floor(size / 2)
Code web/src/core/filters/nonlinear/subregions.ts, kuwaharaFilter
Operator code web/src/core/filters/nonlinear/subregions.ts, kuwaharaRegions
Channels RGB

Overview

The Kuwahara filter splits the window around each pixel into four overlapping quadrants and writes the mean of the quadrant whose values vary the least. Near an edge, the quadrants that cross the edge vary a lot, so a quadrant on one side wins, and the edge stays sharp. Flat areas are averaged. Large windows give a painted look.

Four panels in two rows: disks with Gaussian noise, a 5x5 box filter that blurs the disks, a Kuwahara filter of size 5 with flat disks and sharp edges, and a Kuwahara filter of size 9 with a stronger painted look. Four panels in two rows: disks with Gaussian noise, a 5x5 box filter that blurs the disks, a Kuwahara filter of size 5 with flat disks and sharp edges, and a Kuwahara filter of size 9 with a stronger painted look.
The Kuwahara filter flattens the noise and keeps the disk edges. Larger windows look more painted.

How it works

  1. Split the window into four squares, the top-left, top-right, bottom-left and bottom-right quadrants. Each one holds the center pixel.
  2. Measure the mean and the variance of each quadrant.
  3. Write the mean of the quadrant with the smallest variance.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \mu_{k^*}, \qquad k^* = \operatorname*{arg\,min}_{k \in \{1,2,3,4\}} \sigma_k^2 \]

The formula shows the default parameter values.

where

  • \(\mu_k\): The mean of quadrant k
  • \(\sigma_k^2\): The variance of the luma in quadrant k
  • \(a\): Half the window size, (size - 1) / 2

The window of side 2a + 1 is split into four squares of side a + 1, the top-left, top-right, bottom-left and bottom-right quadrants. Each one holds the center pixel. mu_k and sigma_k squared are the mean and variance of quadrant k. The output is the mean of the quadrant with the smallest variance. On a tie, the first quadrant in that order wins.

The filter is an adaptive mean. Its averaging region is chosen per pixel by the smallest variance. For a window of side 2a + 1, each quadrant is a square of side a + 1. Neighboring pixels can choose different quadrants, which makes the typical blocky, brush-like texture. The variance is measured on luma, so a color image uses one quadrant for all channels.

Kernel or operator

There is no kernel. The Inspector shows the window and the rule. The Learn panel shows a line profile, and the worked example below compares the four quadrants for one pixel.

Algorithm

  1. Pad the image with the border mode.
  2. For each pixel, compute the mean and variance of the luma in each of the four quadrants.
  3. Pick the quadrant with the smallest variance.
  4. Write the mean of each color channel over that quadrant.

Parameters

Parameter Default Values Meaning
Window size size 5 3 to 15, odd px Width of the window. Each quadrant is half of it, plus the center row and column.
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.

Implementation

kuwaharaFilter in web/src/core/filters/nonlinear/subregions.ts lists the quadrants as offsets with kuwaharaRegions. For each pixel, it measures the luma variance of each quadrant and averages each channel over the winner. The Nagao-Matsuyama filter uses the same code with other regions. It is a color filter: the framework in web/src/core/filters/run.ts hands it the red, green and blue planes together, with the border mode, and keeps alpha. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.

Worked example

\[ W = \begin{bmatrix} 20 & 22 & 21 & 200 & 202 \\ 19 & 21 & 20 & 201 & 199 \\ 22 & 20 & 120 & 200 & 203 \\ 21 & 19 & 22 & 198 & 200 \\ 20 & 23 & 21 & 202 & 201 \end{bmatrix} \]

A 5x5 window on a vertical edge, with a noisy center pixel of 120. Each quadrant is 3x3 and holds the center.

Quadrant Mean Variance
Top left 31.67 976.2
Top right 151.78 5547.5
Bottom left 32 969.3
Bottom right 151.89 5479

The two right quadrants cross the edge, so their variance is large. The bottom left quadrant has the smallest variance, so the output is its mean, 32. The edge is not blurred, and the noisy center is pulled to the dark side it belongs to.

Complexity

  • Time: \(O(N\,a^2)\)
  • Memory: \(O(N)\)

N is the number of pixels and a is half the window size. Each pixel reads four quadrants of (a + 1) squared pixels.

Artifacts and limitations

Limitations

  • Makes blocky artifacts in textured areas, because neighboring pixels pick different quadrants.
  • Corners and thin lines are rounded off or removed.

Artifacts

  • Square, brush-like blocks of flat color, larger with larger windows.

Tests

The reference measures every quadrant of each NumPy window. Before it is used, the script checks it against a second computation with shifted box means of the luma from scipy.ndimage.uniform_filter. The test covers sizes 3, 5 and 7 on a color input, for all five border modes.

References

  • Kuwahara, M., Hachimura, K., Eiho, S. and Kinoshita, M. Processing of RI-angiocardiographic images. In Digital Processing of Biomedical Images, Plenum Press, 1976, pages 187 to 202. DOI 10.1007/978-1-4684-0769-3_13.