Median filter


Before starting this lesson, you should be familiar with:

Learning Objectives

After completing this lesson, learners should be able to:
  • Understand in detail what happens when applying a median filter to an image


The median filter is a rank filter and is one of the most popular filters for reducing noise in microscopy images. While the median filter has indeed many good properties, it should be - like any other filter - used with care and a good understanding of its properties.

Concept map

graph TD pixel --> NE("neighbourhood pixel values") NE --> median median --> MF("median filtered pixel value")


Median filter example. Left - Raw; Right - After a 5x5 median filter.


Median filter exploration

Explore the effect of the median filter on various example images. Explore how changing the size (structural element) of the filter affects the result.

Example images:

Show activity for:  

ImageJ Macro

run("Close All");

//File > Open...
// Image > Duplicate...
run("Duplicate...", "title=Median_1");
// Image > Duplicate...
run("Duplicate...", "title=Median_2");
// Image > Duplicate...
run("Duplicate...", "title=Median_5");

// Process › Filters › Median...
run("Median...", "radius=1");

// Process › Filters › Median...
run("Median...", "radius=2");

// Process › Filters › Median...
run("Median...", "radius=5");

skimage napari

# %%
# Median filtering

# %%
# import modules
import napari
import numpy as np
from skimage import filters
from skimage.filters import rank
from skimage.morphology import disk # Structuring element
from OpenIJTIFF import open_ij_tiff

# %%
# Instantiate the napari viewer
viewer = napari.Viewer()

# %%
# Read and view the image
image, *_ = open_ij_tiff('')

# %% 
# Compare small median and mean filter
# - Appreciate that the median filter better preserves the edges
median_1 = filters.median(image, disk(1))
mean_1 = rank.mean(image, disk(1))

# %% 
# Compare a larger median and mean filter
# - Appreciate that a median filter eliminates the small square entirely
median_3 = filters.median(image, disk(3))
mean_3 = rank.mean(image, disk(3))

###########  New image: nucleare speckles #############

# %%
# Clear the napari viewer

# %%
# Load image
image, *_ = open_ij_tiff('')

# %%
# View the image and find the radius of the intra-nuclear speckles

# %%
# Remove small intra-nuclear structures 
# - Appreciate that a median filter removes the intra-nuclear speckles while keeping the nuclear shape intact
# - This can be helpful in many ways, e.g. some deep learning nuclear segmentation tools get confused by intra-nuclear speckles
# - Another application is to subtract the filtered image from the original image to only have the speckles; this will be used in the 
#   "local background subtraction" training module
image_without_speckles = filters.median(image, disk(radius=15))


True or false?

  1. Median filter is just another name for mean filter.
  2. Small structures can completely disappear from an image when applying a median filter.


  1. Median filter is just another name for mean filter. FALSE
  2. Small structures can completely disappear from an image when applying a median filter. TRUE


Properties of median filter

The median filter is based on ranking the pixels in the neighbourhood

In general, for any neighbourhood filter, if the spatial extend of the neighbourhood is significantly (maybe three-fold) smaller than the smallest spatial length scale that you care about, you are on the safe side.

However, in biology, microscopy images are often containing relevant information down to the level of a single pixel. Thus, you typically have to deal with the fact that filtering may alter your image in a significant way. To judge whether this may affect your scientific conclusions you therefore should study the effect of filters in some detail.

Although a median filter typically is applied to a noisy gray-scale image, understanding its properties is easier when looking at a binary image.

From inspecting the effect of the median filter on above test image, one could say that a median filter

  • is edge preserving
  • cuts off at convex regions
  • fills in at concave regions
  • completely removes structures whose shortest axis is smaller than the filter width

Follow-up material

Recommended follow-up modules:

Learn more: