After completing this lesson, learners should be able to:
Understand in detail what happens when applying a median filter to an image
Motivation
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")
Figure
Median filter example. Left - Raw; Right - After a 5x5 median filter.
Explore the effect of the median filter on various example images. Explore how changing the size (structural element) of the filter affects the result.
Observe how the median filter behaves for binary images.
Show activity for:
ImageJ Macro
run("Close All");//File > Open...open("https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit__two_noisy_squares_different_size.tif");// Image > Duplicate...run("Duplicate...","title=Median_1");// Image > Duplicate...run("Duplicate...","title=Median_2");// Image > Duplicate...run("Duplicate...","title=Median_5");selectWindow("Median_1");// Process › Filters › Median...run("Median...","radius=1");selectWindow("Median_2");// Process › Filters › Median...run("Median...","radius=2");selectWindow("Median_5");// Process › Filters › Median...run("Median...","radius=5");run("Tile")
skimage napari
### Median Filtering
importnumpyasnp# Instantiate the napari viewer
importnapariviewer=napari.Viewer()# Read the intensity image
#
# (Replace the image path to explore the other example images)
#
fromOpenIJTIFFimportopen_ij_tiffimage1,axes1,scales1,units1=open_ij_tiff('https://github.com/NEUBIAS/training-resources/raw/master/image_data/xy_8bit_binary__squares_different_size.tif')# Inspect image data type and values
print('image type:',image1.dtype,'\n','image shape:',image1.shape,'\n','intensity min:',np.min(image1),'\n','intensity max:',np.max(image1),'\n')# View the intensity image
viewer.add_image(image1,name='original image1')# First method using the median filter
fromskimageimportfilters# Second method using the mean filter
fromskimage.filtersimportrankfromskimage.morphologyimportdisk# Local median filtering with radius 1
median_1=filters.median(image1,disk(1))viewer.add_image(median_1,name='Median1')# Local mean filtering with radius 1
mean_1=rank.mean(image1,disk(1))viewer.add_image(mean_1,name='Mean1')# Local median filtering with radius 2
Median_2=filters.median(image1,disk(2))viewer.add_image(Median_2,name='Median2')# Local mean filtering with radius 2
mean_2=rank.mean(image1,disk(2))viewer.add_image(mean_2,name='Mean2')# Local median filtering with radius 5
Median_3=filters.median(image1,disk(5))viewer.add_image(Median_3,name='Median3')# Local mean filtering with radius 5
mean_3=rank.mean(image1,disk(5))viewer.add_image(mean_3,name='Mean3')
Assessment
True or false?
Median filter is just another name for mean filter.
Small structures can completely disappear from an image when applying a median filter.
Solution
Median filter is just another name for mean filter. FALSE
Small structures can completely disappear from an image when applying a median filter. TRUE
Explanations
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