Skip to content

Entropy filter

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

Overview

The entropy filter maps how many different values occur in the window around each pixel, and how evenly. A flat window has entropy 0. A window full of different values has a high entropy. Unlike the variance, the entropy ignores how far apart the values are, so a clean black and white pattern has a low entropy and noise has a high one.

Four panels in two rows: a checkerboard and its entropy map, dark except along the square borders, then the Noise patches image and its entropy map, bright in the noisy quadrants. Four panels in two rows: a checkerboard and its entropy map, dark except along the square borders, then the Noise patches image and its entropy map, bright in the noisy quadrants.
The checkerboard has a high variance but a low entropy, because it has only two values. Noise has a high entropy.

How it works

  1. Count how often each value occurs in the window.
  2. Divide the counts by the window size to get shares.
  3. Add up minus each share times its base-2 logarithm.

Mathematical foundation

\[ H(x,y) = -\sum_{v=0}^{255} p_{xy}(v) \log_2 p_{xy}(v) \]

The formula shows the default parameter values.

where

  • \(p_{xy}(v)\): Share of the window with value v

p_xy(v) is the share of the window with the whole value v. Terms with p = 0 count as 0. A flat window has entropy 0. A window whose n values are all different has the largest entropy, log2 n bits. The measure counts how many different values occur and how evenly, not where they are.

This is the Shannon entropy of the local histogram, in bits. A window of n values holds at most log2 n bits, when all n values differ. Values are rounded to whole numbers first, so it counts 256 possible values. The entropy is a first-order texture measure: it uses only how often values occur, not where they are in the window. 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. Round the values to whole numbers and pad the image with the border mode.
  2. Keep a 256-bin histogram of the window as it slides along each row.
  3. Add up -p log2 p over the bins that are not empty.
  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 Stretch to full range 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

localEntropyFilter in web/src/core/filters/adaptive/local-stats.ts reads the sliding 256-bin histogram of histogramPlane, the same one the median and mode filters use, and adds up the entropy terms in histogramEntropy. 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} 30 & 30 & 220 \\ 30 & 30 & 220 \\ 30 & 120 & 220 \end{bmatrix} \]
Value Count Share p -p log2 p
30 5 0.5556 0.4711
120 1 0.1111 0.3522
220 3 0.3333 0.5283

The entropy is the sum, 1.3516 bits. A flat window has 0 bits, and a window of 9 different values has log2 9 = 3.1699 bits.

Complexity

  • Time: \(O(N\,(k + 256))\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and k the window width. Each step updates one column of the histogram and scans its 256 bins.

Artifacts and limitations

Limitations

  • Gaussian noise raises the entropy everywhere.
  • Small windows hold few values, so the entropy is coarse.

Artifacts

  • Blocky responses at region borders, as wide as the window.

Tests

The reference counts the values of each NumPy window. Before it is used, it is checked against skimage.filters.rank.entropy away from the image edge, where both see the same pixels. The test covers window sizes 3 and 5 for all five border modes.

References