Median Filters

The MedianFilter Process

Box Size

Kernel Type

Amount

Number of Iterations

The MedianFilter Process

The median of a distribution is the value for which larger and smaller values are equally probable. To calculate the median of a list of sample values, sort them in any order, and then peek the central value, or the mean between the two central values if the list is even-sized. If your list of values has a strong central tendency, which manifests itself as a single, well defined peak on the histogram, then the median is a good estimator of the peak position. If the distribution has no central peak, or if it is bimodal (two peaks), then the median is mostly meaningless.

Applied to images, median calculation leads to a useful morphological filter. First define a neighborhood for each pixel in the image. This can be as simple as taking a square box centered on each pixel, or can be somewhat more complex. Anyway, each pixel will have its own associated neighborhood, which will consist on a given number of surrounding source pixels. Now calculate the median of each neighborhood and store all of them in a safe place. Finally, replace each pixel with the median value of its associated neighborhood.

Among other applications, a median filter can be a useful tool for noise reduction. Why? There is a type of noise, known as impulsional noise, that is characterized by bright and/or dark high-frequency (small in relative scale) features appearing randomly over the image. Statistically, impulsional noise falls well outside the peak of the distribution of any given pixel neighborhood, so the median is well suited to learn where impulsional noise is not present, and hence to remove it by exclusion.

Stellar objects can be interpreted as a sort of impulsional noise in deep-sky images, especially when they are numerous: they are more or less randomly distributed, they are bright, and they are small. Taking advantage of this fact, the median filter can be used to remove or isolate stars on deep-sky astrophotos, usually with the purpose of building masks.

The MedianFilter process in PixInsight is defined by the following parameters:

Box Size

This is the odd size of the neighborhood considered for each pixel. The smallest box size is 3 pixels.

Kernel Type

This refers to the shape and functionality of the kernel (pixel neighborhood) used. A square kernel is used by default, but circular kernels and two-way hybrid kernels are also available. These kernels are explained in the figures below. Shaded squares correspond to pixels used to calculate median values, and white squares correspond to pixels that are ignored in the filtering process.   Square kernel, box size = 5 Circular kernel, box size = 5 Circular kernel, box size = 7 Two-way hybrid median kernel, box size = 5. Two median values are calculated: Mr is the median of horizontal and vertical "R" pixels, and Md is the median of diagonal "D" pixels. The filter value is the arithmetic mean of the two median values and the central pixel C, that is, (Mr + Md + C) / 3

A hybrid median filter preserves edges much better than a square-box median filter because it's a three-step ranking operation: data from different spatial directions are ranked separately. Circular kernels can help preserving small structures, especially rounded ones, like stars.

Amount

The Amount parameter modulates the filtering process by mixing filtered and original pixels. If m is the median-filtered pixel value corresponding to an original pixel value f, and a is the Amount parameter, then the resulting pixel value g is given by:

g  =  a × m  +  (1 — a) × f

where pixel values are in the normalized [0,1] interval.

Number of Iterations

In PixInsight, morphological filters can be applied recursively. This is useful when combined with modulation, that is, when the Amount parameter is less than one. When a median filter is applied this way, a fraction of the original unprocessed data is feeded into the algorithm at the beginning of each successive iteration, and the whole process converges to a final result that can yield better detail and contrast preservation.

The Median Filter Window

With the information given in the preceding section, the Median Filter window mostly explains by itself: The Use Luminance option, when selected, will apply the median filter to the luminance of a RGB color image (for grayscale images this is ignored), making use of the associated RGB working space. Luminance is calculated on the fly in the CIE L*a*b* space, the median filter is applied, and the reverse transform is performed back to the RGB space.