STScI Logo

fmode images.imfilter



fmode -- quantize and box modal filter a list of images


fmode input output xwindow ywindow


List of input images.
List of filtered images. The number of input images must be the same as the number of output images. If the input image name equals the output image name the filtered image replaces the original image.
xwindow, ywindow
The size of the modal filter. Xwindow and ywindow must be odd. Even values for xwindow or ywindow will be rounded up to the nearest odd integer.
hmin = -32768, hmax = 32767
The histogram quantization parameters. Hmin and hmax define the minimum and maximum permitted values for the integer representation of the input image. The default values are those suitable for the 16 bit twos complement data produced by current CCDs. Hmin and hmax should be chosen so as to minimize the space required to store the image histogram.
zmin = INDEF, zmax = INDEF
The quantization parameters. Zmin and zmax default to the minimum and maximum pixel values in the input image. Pixel values from zmin to zmax are linearly mapped to integer values from hmin to hmax. If zmin = hmin and zmax = hmax, the image pixels are converted directly to integers. Image values less than or greater than zmin or zmax will default to hmin and hmax respectively.
zloreject = INDEF, zhireject = INDEF
The minimum and maximum good pixel values. Zloreject and zhireject default to zmin and zmax in the input data or equivalently to hmin and hmax in the integer representation of the input image.
unmap = yes
Fmode rescales the integer values to numbers between zmin and zmax by default. If the user wishes to preserve the mode of the quantized images, the unmap parameter should be set to no.
boundary = "nearest"
The type of boundary extension. The options are:
Use the value of the nearest pixel.
Use a constant value.
Reflect pixel values around the boundary.
Wrap pixel values around the boundary.
constant = 0.
The value for constant valued boundary extension.
verbose = yes
Print messages about actions taken by the task ?


FMODE takes a list of input images input and produces a set of filtered output images output . The filter consists of a sliding rectangular xwindow by ywindow window whose function is to replace the center pixel in the window with the mode of the pixels in the window. The mode is defined in the expression below.

	mode = 3. * median - 2. * mean

The median of a sequence of numbers is defined to be the value of the (n + 1) / 2 pixel in the ordered sequence. Out-of-bounds pixel references are handled by setting the parameter boundary .

If zmin = hmin and zmax = hmax , FMODE converts the image pixels directly to integers. This operation may result in truncation of the pixel values if the input image is not an integer image. Otherwise the input pixel values from zmin to zmax are linearly mapped to integer values from hmin to hmax. The histogram, median, and number of pixels less than the median, are computed for the first window position. These quantities are then updated as the median filter moves one position and the mode is recomputed. The unmap parameter is normally set so as to restore the output pixel values to th range defined by zmin and zmax, but may be turned off if the user wishes to examine the quantized pixels. The precision of the mode in integer space and pixel space is 1.0 and (zmax - zmin) / (hmax - hmin) respectively.

The zloreject and zhireject parameters may be used to reject bad data from the modal filtering box. If no good data is left in a given filtering box, then the mode is set to zloreject if the majority of the pixels are less than zloreject, or to zhireject if the majority of pixels are greater than zhireject.


A description of the fast median algorithm used here can be found in "Topics in Applied Physics: Two-Dimensional Digital Signal Processing II: Transforms and Median Filters", Volume 43, 1981, Springer-Verlag, edited by T.S. Huang, page 209.

A derivation of the expression for the mode used here can be found in "Statistics in Theory and Practice", Robert Lupton, 1993, Princeton University Press, problem 2.


1. Modal filter a 16 bit CCD image using a 5 by 5 window.

   im> fmode input output 5 5 hmin=-32768 hmax=32767 zmin=-32768. \
   >>> zmax=32767.

2. Modal filter a KPNO PDS image using a 3 by 3 window.

   im> fmode input output 3 3 hmin=0 hmax=4095 zmin=0. zmax=4095.

3. Modal filter an 8 bit image using a 3 by 3 image.

   im> fmode input output 3 3 hmin=0 hmax=255 zmin=0. zmax=255.

4. Modal filter an image with real values from 0.0 to 1.0 with a precision of .003.

   im> fmode input output 5 5  hmin=0 hmax=1000 zmin=0. \
   >>> zmax=1.

5. Modal filter the test image dev$pix rejecting any pixels < 5 or greater than 19935 from the mode computing process.

    im> fmode dev$pix output 5 5 hmin=-1 hmax=20000 zmin=-1.0 \
    >>> zmax=20000 zloreject=5 zhireject=20000


It requires approximately 6.1 and 7.6 CPU seconds to modal filter a 512 by 512 square integer image with a 5 by 5 and 7 by 7 window respectively (SPARCStation2).


This technique is most suitable for integer data and data which has not been calibrated. For non-integer data the calculated median is an approximation only.

If the dynamic range of the data defined by hmin and hmax is large the memory requirements can become very large.


mode, rmode, frmode

Source Code · Search Form · STSDAS