# 16.16 Median Filter

## Description

The medianflt2 X-Function filter examines the N by N pixels centered on each cell of a matrix, finds the median value of the N by N cells, and then replaces the central cell value with the median value. The filter is useful for removing spot noise (white spots, black spots) from an image because noisy pixels typically have values that depart significantly from the median cell values.

To use this function:

1. Create a new matrix with data.
2. Activate the matrix.
3. Select Analysis: Mathematics: Median Filter. This opens the medianflt2 dialog.

## Dialog Options

Recalculate Controls recalculation of analysis results None Auto Manual For more information, see: Recalculating Analysis Results The input Matrix. For help with range controls, see: Specifying Your Input Data The output matrix. For help with the range controls, see: Output Results Specify the filter window size. Specifies how to pad cells outside borders. ZeroPadding Pads with zeros. MapPadding Uses reflection padding. RepeatPadding Repeats edge values.

## Algorithm

This function examines an $N\times N\!$ matrix centered on each cell of a matrix, finds the median value of the N by N cells, and then replaces the central cell value with the median value.

For example:

Source matrix: $\begin{pmatrix}2 & 3& 4 &5\\3 & 4 &5 &6\\4 & 5 &6 &7\\5 & 6 & 7 &8 \end{pmatrix}$

For a 3 X 3 filter matrix, centered on cell (2,2), the matrix is: $\begin{pmatrix}2 & 3& 4 \\3 & 4 &5 \\4 & 5 &6 \end{pmatrix}\rightarrow(\overbrace{2,3,3,4},4,\overbrace{4,5,5,6})$

The median value of the filter matrix is $4\!$; therefore cell(2,2) in the output matrix remains $4\!$.

When applying filtering to the edge of source matrix, the filter may fall outside the matrix. In such cases, we can pad the area outside the source matrix with zeros (Zero Padding), by reflection padding (Map Padding), or by repeating edge values (Repeat Padding).