Skip to content

Nagao-Matsuyama 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 2
Code web/src/core/filters/nonlinear/subregions.ts, nagaoMatsuyamaFilter
Operator code web/src/core/filters/nonlinear/subregions.ts, nagaoRegions
Channels RGB

Overview

The Nagao-Matsuyama filter is a cousin of the Kuwahara filter with nine subregions in a 5x5 window instead of four quadrants. Eight of them point outward, to the four sides and the four corners, so a region fits on one side of an edge in any of eight directions. The filter writes the mean of the calmest region. It keeps edges and corners and is less blocky than the Kuwahara filter.

Three panels: disks with Gaussian noise, the result of one pass of the Nagao-Matsuyama filter, and the result of three passes with smoother flat areas and round disk edges. Three panels: disks with Gaussian noise, the result of one pass of the Nagao-Matsuyama filter, and the result of three passes with smoother flat areas and round disk edges.
One pass smooths a little. Three passes smooth more, and the small disks keep their round shape.

How it works

  1. Look at nine shapes in the 5x5 window: the center 3x3 square, four pentagons that point to the sides and four hexagons that point to the corners.
  2. Measure the mean and the variance of each shape.
  3. Write the mean of the shape with the smallest variance.

Mathematical foundation

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

The formula shows the default parameter values.

where

  • \(\mu_k\): The mean of subregion k
  • \(\sigma_k^2\): The variance of the luma in subregion k

The 5x5 window around the pixel holds nine subregions: the center 3x3 square, four pentagons that point north, east, south and west, and four hexagons that point to the corners. Each pentagon and hexagon has 7 pixels, including the center. mu_k and sigma_k squared are the mean and variance of subregion k. The output is the mean of the subregion with the smallest variance.

Each pentagon and hexagon holds 7 pixels, including the center pixel. A pentagon covers two rows or columns on one side of the center. A hexagon covers a corner of the window. These shapes avoid the pixels on the other side of an edge in eight directions. That is why the filter keeps diagonal edges and corners better than the four Kuwahara quadrants. The window is small, so stronger smoothing comes from repeating the filter.

Kernel or operator

There is no kernel. The Inspector shows the window and the rule. The worked example below lists all nine regions for one pixel on a diagonal edge.

Algorithm

  1. Pad the image with the border mode.
  2. For each pixel, compute the mean and variance of the luma in the nine subregions.
  3. Pick the subregion with the smallest variance. On a tie, the first in the list wins.
  4. Write the mean of each color channel over that subregion.

Parameters

Parameter Default Values Meaning
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

nagaoMatsuyamaFilter in web/src/core/filters/nonlinear/subregions.ts builds the nine regions in nagaoRegions, by turning one pentagon and one hexagon through four quarter turns. It shares the selection code with the Kuwahara filter. 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 & 20 & 20 & 21 & 200 \\ 21 & 19 & 20 & 200 & 201 \\ 20 & 22 & 60 & 199 & 200 \\ 19 & 200 & 201 & 200 & 202 \\ 200 & 199 & 201 & 202 & 200 \end{bmatrix} \]

A diagonal edge runs from the top right to the bottom left. The center pixel, 60, lies on the dark side.

Subregion Mean Variance
Center 124.56 7246.7
North 51.43 3869.7
East 180.29 2412.2
South 180.43 2418
West 51.57 3861.4
North-west 26 193.4
North-east 128.71 6924.5
South-east 180.57 2424
South-west 128.71 6925.1

The north-west subregion lies on the dark side and varies the least, so the output is its mean, 26. The east, south and south-east shapes reach across the edge and vary much more.

Complexity

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

N is the number of pixels. Each pixel reads nine subregions of 7 or 9 pixels.

Artifacts and limitations

Limitations

  • The window is fixed at 5x5, so it smooths only a little in one pass. Repeat it for more.
  • Fine texture becomes flat.

Artifacts

  • Small flat patches in textured areas.

Tests

The reference builds the same nine regions with NumPy and checks that they are all different, with 9 and 7 pixels. The test compares the filter on a color input for all five border modes.

References

  • Nagao, M. and Matsuyama, T. Edge preserving smoothing. Computer Graphics and Image Processing 9(4), 394 to 407, 1979. DOI 10.1016/0146-664X(79)90102-3.