# Median Filter in Image Processing

The** Median Filter in Image Processing** is normally used to reduce noise in an image, somewhat like the mean filter. However, it often does a better job than the mean filter of preserving useful detail in the image.

### What is the Median Filter in Image Processing? Definition

**Median Filter** is a simple and powerful non-linear filter.

- It is used for reducing the amount of intensity variation between one pixel and the other pixel.
- In this filter, we replace pixel value with the median value.
- The median is calculated by first sorting all the pixel values into ascending order and then replace the pixel being calculated with the middle pixel value
- Salt and pepper noise.

**Mean filtering** is a simple, intuitive and easy to implement a method of s*moothing* images, *i.e.* reducing the amount of intensity variation between one pixel and the next. It is often used to reduce noise in images.

### How to work the Median Filter in Image Processing

The median filter considers each pixel in the image in turn and looks at its nearby neighbors to decide whether or not it is representative of its surroundings.

Instead of simply replacing the pixel value with the *mean* of neighboring pixel values, it replaces it with the ** median **of those values.

The median is calculated by first sorting all the pixel values from the surrounding neighborhood into numerical order and then replacing the pixel being considered with the middle pixel value. (If the neighborhood under consideration contains an even number of pixels, the average of the two middle pixel values is used.)

**Also Read:** Mean Filter in Image Processing

**Example 1: 3×3 Median Filter**

2D Median filtering example using a 3 x 3 sampling window:

Keeping border values unchanged

Extending border values outside with values at the boundary

Extending border values outside with 0s

**Example 2: 3×3 Median Filter**

then,