MEDIAN Function

Finds the median value of an array, or applies a one- or two-dimensional median filter of a specified width to an array.

Usage

    result = MEDIAN(array[, width])

Input Parameters

array—The array to be processed. May be of any size, dimension, and data type, except string.

width—(optional) The length of the one- or two-dimensional neighborhood to be used for the median filter. Must be a scalar value, greater than 1 and less than the smaller of the dimensions of array. The neighborhood will have the same number of dimensions as array.

Returned Value

result—The median value for array, or array after a median filter has been applied to it.

  • If width is specified and array is of a byte data type, the result is also a byte type. All other types are converted to double-precision floating-point to preserve precision during calculations. The returned result is converted to single-precision floating-point to preserve backwards compatibility with previous PV-WAVE versions, unless the Same_Type keyword is set. If width is used, array can have only one or two dimensions.

  • If width is not specified, array may have any valid number of dimensions. It is converted to single-precision floating-point, and the median value is returned as a floating-point value.

Keywords

Average—If specified, and the number of elements in array is even, and result returns the average of the two middle values.

Note: The Average keyword is ignored when filtering a byte array.

Edge—A scalar string indicating how edge effects are handled. (Default: 'copy') Valid strings are:

    'zero'—Sets the border of the output image to zero.

    'copy'—Copies the border of the input image to the output image. (Default)

Same_Type—If set, the output image is the same data type as the input image.

Discussion

The MEDIAN function supports multi-layer band interleaved images. When the input array is 3D and the width parameter is given, it is automatically treated as an array of images, array(m, n, p), where p is the number of m-by-n images. Each image is then operated on separately and an array of the result images is returned.

Median smoothing replaces each point with the median of the one- or two-dimensional neighborhood of the given width. It is similar to smoothing with a boxcar or average filter, but does not blur edges larger than the neighborhood. In addition, median filtering is effective in removing “salt and pepper” noise (isolated high or low values).

The scalar median is simply the middle value, which should not be confused with the average value (e.g., the median of [1, 10, 4] is 4, while the average is 5).

Example

This example exhibits median filtering of an image that has been corrupted with noise spikes. (For this procedure to run, PV-WAVE IMSL Mathematics must be running.) The results can be seen in Results of MEDIAN Function, which shows the original image on the left, the corrupted image in the center, and the median filtered image on the right.

; Open the file containing the human head dataset.
OPENR, unit, !Data_dir + 'head.img', /Get_Lun
; Create an array large enough to hold the dataset.
head = BYTARR(512, 512)
; Read the data, then close the file and free the file
; unit number.
READU, unit, head
FREE_LUN, unit
; Use REFORM to remove degenerate dimensions, then resize
; the image using the CONGRID function.
slice = CONGRID(REFORM(head), 256, 256, /Interp)
; Create a window large enough to display three images of
; the size of the original image.
WINDOW, 0, Xsize=768, Ysize=256
; Load the red temperature color table.
LOADCT, 3
; Display the original image in the leftmost portion of window.
TVSCL, slice, 0
; Histogram equalize the color table.
HIST_EQUAL_CT, slice
; Initialize the IMSL Mathematics toolkit
math_init
.RUN
FOR i=0L, 253, 6 DO BEGIN
   rows = RANDOM(256) GT 0.5
   k = RANDOM(1)
   h = 3 * (k(0) GT 0.5)
   ; This FOR loop creates 3-by-3 isolated holes in the image.
   FOR j=h, 253, 6 DO BEGIN
      IF rows(j) THEN slice(i:i + 2, j:j + 2) = 0
   ENDFOR
ENDFOR
END
; Display the image with holes in the center portion of window.
TVSCL, slice, 256, 0
; Median filter the image with holes.
filtered = MEDIAN(slice, 5)
; Display the median filtered image in the rightmost portion
; of the window.
TVSCL, filtered, 512, 0

Results of MEDIAN Function

See Also

AVG,  LEEFILT,  MAX,  MIN,  SMOOTH