MEDIAN

The MEDIAN function computes the median value. In an ordered set of values, the median is a value with an equal number of values above and below it. Median smoothing replaces each point with the median of the one- or two-dimensional neighborhood of a 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 the middle value, which should not be confused with the average value (e.g., the median of the array [1,10,4] is 4, while the average is 5.)

Note: The MEDIAN function treats NaN values as missing data.

Examples

; Create a simple image and display it:

D = SIN(DIST(200)^0.8) & TVSCL, D



; Display D median-filtered with a width of 9:

TVSCL, MEDIAN(D, 9)



; Print the median of a four-element array, with and without

; the EVEN keyword:

PRINT, MEDIAN([1, 2, 3, 4], /EVEN)

PRINT, MEDIAN([1, 2, 3, 4])

IDL prints:

2.50000

3.00000

Syntax

Result = MEDIAN ( Array [, Width] [, /DOUBLE] [, DIMENSION=value] [, /EVEN] )

Return Value

Returns the median value of Array if one parameter is present, or applies a one- or two-dimensional median filter of the specified width to Array and returns the result.

Arguments

Array

The array to be processed. If Width is also supplied, and Array is of byte type, the result is of byte type. If Array is double precision or double complex, the computation is done using double precision and the result is double-precision. All other types are converted to single-precision floating-point, and the result is floating-point, unless the DOUBLE keyword is set. Array can have only one or two dimensions.

If Width is not given, Array can have any valid number of dimensions. The array is converted to single-precision floating-point, and the median value is returned as a floating-point value.

Width

The size of the one or two-dimensional neighborhood to be used for the median filter. The neighborhood has the same number of dimensions as Array.

Keywords

DOUBLE

Set this keyword to force the computation to be done using double-precision arithmetic and to return a double-precision result.

DIMENSION

Set this keyword to the dimension over which to find the median values of an array. If this keyword is not present or is zero, the median is found over the entire array and is returned as a scalar value. If this keyword is present and nonzero, the result is a “slice” of the input array that contains the median value elements, and the return value will be an array of one dimension less than the input array.

For example, if the dimensions of Array are N1, N2, N3, and DIMENSION = 2, the dimensions of the result are (N1, N3), and element (i,j) of the result contains the median value of Array[i, *, j]. This keyword is ignored if the Width argument is present.

EVEN

If the EVEN keyword is set when Array contains an even number of points (i.e. there is no middle number), MEDIAN returns the average of the two middle numbers. The returned value may not be an element of Array. If Array contains an odd number of points, MEDIAN returns the median value. The returned value will always be an element of Array—even if the EVEN keyword is set—since an odd number of points will always have a single middle value.

Version History

Original

Introduced

5.6

Added DIMENSION keyword

6.1

Added DOUBLE keyword

See Also

DIGITAL_FILTER, LEEFILT, MOMENT, SMOOTH