Median Filters 





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 evensized. 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 highfrequency (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 deepsky 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 deepsky astrophotos, usually with the purpose of building masks. The MedianFilter process in PixInsight is defined by the following parameters: This is the odd size of the neighborhood considered for each pixel. The smallest box size is 3 pixels. This refers to the shape and functionality of the kernel (pixel neighborhood) used. A square kernel is used by default, but circular kernels and twoway 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.
A hybrid median filter preserves edges much better than a squarebox median filter because it's a threestep ranking operation: data from different spatial directions are ranked separately. Circular kernels can help preserving small structures, especially rounded ones, like stars. The Amount parameter modulates the filtering process by mixing filtered and original pixels. If m is the medianfiltered 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. 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. 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. 