Skip to content

Rank-order filter

Fact Value
Also called Percentile filter, Order-statistic filter
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, spatial, order statistic
Output Image
Operator shown as rank (exact)
Support radius floor(size / 2)
Code web/src/core/filters/nonlinear/rank-order.ts, rankOrderFilter
Operator code web/src/core/filters/nonlinear/rank-order.ts, chosenRank
Channels RGB, Brightness only

Overview

The rank-order filter sorts each window and keeps the value at a chosen position. You give the position as a rank, from 1 to the number of values, or as a percentile from 0 to 100. Rank 1 is the min filter, the middle rank is the median and the last rank is the max filter.

Three panels of disks with salt-and-pepper noise after a 3x3 rank-order filter: rank 1 is dark with shrunken disks, rank 5 is clean, rank 9 is bright with grown disks. Three panels of disks with salt-and-pepper noise after a 3x3 rank-order filter: rank 1 is dark with shrunken disks, rank 5 is clean, rank 9 is bright with grown disks.
Low ranks darken and shrink bright shapes. High ranks brighten and grow them.

How it works

  1. Take the square window around the pixel.
  2. Sort its values from dark to bright.
  3. Keep the value at the chosen rank.

Mathematical foundation

\[ I_{\text{out}}(x,y) = W_{(k)}, \qquad k = \begin{cases} \text{the rank} & \text{by rank} \\ \lfloor n p / 100 \rfloor + 1 & \text{by percentile, } p < 100 \\ n & p = 100 \end{cases} \]

The formula shows the default parameter values.

where

  • \(W_{(k)}\): The k-th smallest value of the window
  • \(n\): Number of values in the window
  • \(p\): Percentile from 0 to 100
  • \(k\): Rank from 1 to n

W is the list of the n = size squared values in the window. W with index (k) is its k-th smallest value. Rank 1 gives the min filter, rank n gives the max filter, and rank (n + 1) / 2 gives the median. The percentile rule is the one of scipy.ndimage.percentile_filter.

The k-th order statistic of the window is a nonlinear filter. It commutes with any increasing change of the values, such as a gamma curve, apart from rounding. A percentile p picks the 0-based position floor(n p / 100), and p = 100 picks the last value. This is the rule of scipy.ndimage.percentile_filter.

Kernel or operator

The Inspector shows the window, the number of values and the rank that is used. The Rank stepper in the Learn panel shows the sorted window at any pixel and the value the filter gives there.

Algorithm

  1. Turn the percentile into a rank, if it is given.
  2. Pad the image with the border mode.
  3. Keep a 256-bin histogram of the window as it slides along each row.
  4. Move a pointer to the value that holds rank k and write it to the output pixel.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 25, odd px Width and height of the square window.
Choose by by Rank Rank, Percentile Give the position in the sorted window as a rank number or as a percentile.
Rank rank 5 1 to 625 Used when Choose by is Rank. Position in the sorted window, from 1 (the smallest) to the number of values. Larger ranks are lowered to the last one.
Percentile percentile 50 0 to 100 % Used when Choose by is Percentile. Position in the sorted window as a percentage. 0 picks the smallest value and 100 the largest.
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

rankPlane in web/src/core/filters/nonlinear/window.ts keeps a 256-bin histogram of the window as it slides along each row (Huang, Yang and Tang). A pointer marks the value that holds rank k, and it moves only as far as the window changes. The median filter uses the same code. Values are whole numbers. With the brightness scope, luma is rounded first. 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 rounding 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 & 255 & 63 \\ 55 & 0 & 60 \end{bmatrix} \]

Sorted: 0, 49, 52, 55, 58, 60, 61, 63, 255.

Setting Rank Output
Rank 1 1 0
Rank 3 3 52
Rank 5 (median) 5 58
Percentile 75 floor(9 x 0.75) + 1 = 7 61
Rank 9 9 255

Ranks 2 to 8 skip both impulses.

Complexity

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

N is the number of pixels per channel and k is the window width. The histogram changes by one column per step, and the rank pointer moves only as far as the window changes.

Artifacts and limitations

Limitations

  • Ranks away from the middle make the whole image darker or brighter.
  • Values are rounded to whole numbers.

Artifacts

  • Square blocks with large windows and extreme ranks, as with min and max.

Tests

The filter is compared with scipy.ndimage.rank_filter and scipy.ndimage.percentile_filter for several ranks and percentiles with window sizes 3 and 5 and all five border modes. The results match exactly. Unit tests check the percentile rule and that large ranks are lowered to the last one.

References