Median Filter

The median filter is perhaps the most intuitive filter there is. In implementation, the median filter replaces the center pixel with the median pixel value around the neighborhood defined by the kernel size. That is, if the kernel size is $3x3$, then the center pixel for every $3x3$ block on the image is replaced by the median value of the pixels on that block. Let's demonstrate this with an example.

Suppose we have a $2D$ image of the following pixel and for simplicity, let's apply median filter on coordinate $A_{(2,2)}$ in bold with a kernel size $3,3$:

$$ A = \begin{pmatrix} 1 & 1 & 3 & 5 \\ 3 & \textbf{2} & 5 & 2 \\ 2 & 7 & 6 & 1 \\ 1 & 1 & 7 & 2 \\ 4 & 2 & 5 & 3 \end{pmatrix}, kernel_{(3,3)} $$

To compute the median filter for pixel at $A_{(2,2)}$, we order the neighborhood pixels in the $(3x3)$ neighborhood, determine the median, and replace the $A_{(2,2)}$ by the median. In this case the neighborhood is:

$$ A = \begin{pmatrix} \textbf{1} & \textbf{1} & \textbf{3} & 5 \\ \textbf{3} & \textbf{2} & \textbf{5} & 2 \\ \textbf{2} & \textbf{7} & \textbf{6} & 1 \\ 1 & 1 & 7 & 2 \\ 4 & 2 & 5 & 3 \end{pmatrix} = k_{[3,3]} = [1, 1, 3, 3, 2, 5, 2, 7, 6] = [1, 1, 2, 2, 3, 3, 5, 6, 7] $$

The median value is then $3$. Thus the resulting median filter at $A_{(2,2)}$ is:

$$ A = \begin{pmatrix} 1 & 1 & 3 & 5 \\ 3 & \textbf{3} & 5 & 2 \\ 2 & 7 & 6 & 1 \\ 1 & 1 & 7 & 2 \\ 4 & 2 & 5 & 3 \end{pmatrix}, kernel_{(3,3)} $$

Expanding the median filter across all pixels, the resulting $2D$ matrix becomes:

$$ A = \begin{pmatrix} 1 & 1 & 3 & 5 \\ 3 & 2 & 5 & 2 \\ 2 & 7 & 6 & 1 \\ 1 & 1 & 7 & 2 \\ 4 & 2 & 5 & 3 \end{pmatrix}, kernel_{(3,3)} = \begin{pmatrix} \textbf{1} & \textbf{2} & \textbf{3} & \textbf{5} \\ \textbf{2} & \textbf{3} & \textbf{3} & \textbf{3} \\ \textbf{2} & \textbf{3} & \textbf{2} & \textbf{2} \\ \textbf{2} & \textbf{4} & \textbf{3} & \textbf{3} \\ \textbf{2} & \textbf{4} & \textbf{3} & \textbf{3} \end{pmatrix} $$

Implementation in Python

Let's implement the median filter in python. We will use numpy and skimage to execute a median filter.

import numpy as np
from skimage import filters
A = np.array([ [1, 1, 3, 5],
[3, 2, 5, 2],
[2, 7, 6, 1],
[1, 1, 7, 2],
[4, 2, 5, 3]])
filters.median(A)
array([[1, 2, 3, 5],
    [2, 3, 3, 3],
    [2, 3, 2, 2],
    [2, 4, 3, 3],
    [2, 4, 3, 3]])