## MEDIAN

The MEDIAN function returns the median value (element n /2) 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. 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 simply 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.)

### Calling Sequence

Result = MEDIAN( Array [, Width] )

### Arguments

#### Array

The array to be processed. If Width is also supplied, and Array is of byte type, the result is of byte type. All other types are converted to single-precision floating-point, and the result is floating-point. 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

#### 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.

### Example

Create a simple image and display it by entering:

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

Display D median-filtered with a width of 9 by entering:

TVSCL, MEDIAN(D, 9)

Print the median of a six-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