Skip to content

Standard deviation filter

Fact Value
Also called Local standard deviation
Status Implemented and tested
Family Adaptive and statistical filters
Engine Browser
Type Nonlinear, statistical, local second moment
Output Features
Operator shown as estimator (exact)
Support radius floor(size / 2)
Code web/src/core/filters/adaptive/local-stats.ts, localStdFilter
Operator code web/src/core/filters/adaptive/local-stats.ts, localStdFilter
Channels RGB, Brightness only

Overview

The standard deviation filter maps how far the values in the window typically are from their mean, in levels. It is the square root of the local variance, so it has the same unit as the image. In a flat area with noise, it measures the noise level.

Two panels: bright disks on a dark background, and the 5x5 standard deviation map, black inside the disks and bright rings on their edges. Two panels: bright disks on a dark background, and the 5x5 standard deviation map, black inside the disks and bright rings on their edges.
The standard deviation is near 0 inside the disks and high on their edges.

How it works

  1. Take the window around the pixel.
  2. Find the variance of its values.
  3. Write the square root of the variance.

Mathematical foundation

\[ \sigma(x,y) = \sqrt{\frac{1}{n} \sum_{w \in W_{xy}} w^2 - \Bigl(\frac{1}{n} \sum_{w \in W_{xy}} w\Bigr)^2} \]

The formula shows the default parameter values.

where

  • \(W_{xy}\): The window around (x, y)
  • \(n\): Number of values in the window

The square root of the population variance of the window. It has the same unit as the image, so a value of 20 means the neighbors spread about 20 levels around their mean.

For 8-bit values the standard deviation is at most 127.5, reached when half the window is 0 and half is 255. So the default mapping clips and needs no stretch. In a flat region with additive Gaussian noise of standard deviation sigma, the expected value of the local standard deviation is close to sigma for large windows. The result is a new quantity, not an image, so it goes through the output mapping. With the brightness scope the map is gray.

Kernel or operator

There is no kernel. The Inspector shows the window and the statistic. The Rank stepper in the Learn panel shows the window values at any pixel, sorted, next to the value the filter gives there.

Algorithm

  1. Pad the image with the border mode.
  2. Sum the values and their squares over each window with running sums in float64.
  3. Compute the variance and take its square root.
  4. Map the result to 8 bits with the output mapping.

Parameters

Parameter Default Values Meaning
Window size size 5 3 to 25, odd px Width and height of the square window.
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 Which part of the color image the filter changes.

Implementation

localStdFilter in web/src/core/filters/adaptive/local-stats.ts takes the square root of the variance from localMoments, which uses running sums in float64. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts. It handles the border mode, the channel scope, alpha and the mapping to 8 bits. 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} 52 & 61 & 58 \\ 49 & 70 & 63 \\ 55 & 66 & 60 \end{bmatrix} \]

The variance of the window is 39.556, so the standard deviation is its square root, 6.289 levels. The values spread about 6 levels around their mean of 59.33.

Complexity

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

N is the number of pixels per channel. Running sums make the cost independent of the window size.

Artifacts and limitations

Limitations

  • Edges and texture both give high values.
  • A window that holds an edge reports the edge contrast, not the noise.

Artifacts

  • Bright bands along edges, as wide as the window.

Tests

The reference takes the square root of numpy.var of each window, checked against scipy.ndimage.generic_filter. The test covers window sizes 3 and 5 for all five border modes.

References