Median Filter
Replace each pixel by the median of its neighborhood so salt-and-pepper noise disappears without averaging everything into mush.
Filtering & convolution is nested under Foundations , so the broader pipeline usually still applies here.
Interactive playground
Tweak the operator or scene live so the article connects to an immediate visual result.
Median filter
Sort each local neighborhood and keep the middle value so isolated outliers disappear without averaging every edge away.
Noisy grayscale source
Median-filtered output
96 × 96 live previewIncrease the kernel radius to remove larger specks, then compare how much structure survives.
Family
Foundations -> Filtering & convolution
Neighborhood sampling, blur kernels, and other continuous-image transforms.
Builds on
1 topic
Read these first if you want the surrounding pipeline context.
Unlocks
2 next topics
Use these follow-ups when you want to keep turning the image-processing pipeline forward.
Learning paths
1
This topic appears in curated graphics progressions so the next step is obvious.
Choose this over that
Median, Gaussian, or bilateral?
These three filters all smooth images, but they preserve structure in very different ways.
Median Filter
Choose this when: Impulse noise or isolated salt-and-pepper pixels are the main problem.
Choose something else when: The image needs softer Gaussian-like smoothing or edge-aware denoising.
Gaussian Blur
Choose this when: A stable, isotropic weighted blur is enough and softening edges is acceptable.
Choose something else when: Impulse noise dominates or edges must stay crisper.
Bilateral Filter
Choose this when: You want smoothing but still care about preserving strong intensity boundaries.
Choose something else when: A cheaper blur is sufficient or the image is already binary.
Problem
A weighted blur is great for continuous noise, but it is bad at isolated outliers. One bright dead pixel or one dark speck gets smeared into its neighbors instead of disappearing.
Intuition
The median filter does not average values. It sorts the neighborhood and keeps the middle one, so single extreme outliers lose their leverage while real local structure survives.
Core idea
- Pick a neighborhood such as 3 x 3 around the current pixel.
- Collect those intensity values and sort them.
- Write the middle value back to the output pixel.
- On grayscale data that is a direct median. On RGB data you usually filter channels separately or work in a different representation.
Worked example
If a local patch is mostly around 110 to 120 but one pixel spikes to 255, the median still lands near the cluster of normal values instead of getting pulled upward the way an average would.
Complexity
For an image with W x H pixels and a k x k neighborhood, a direct implementation costs O(WH k^2 log(k^2)) if you literally sort each window. Small fixed kernels are still practical, and specialized implementations can do better.
When to choose it
- Choose it when impulse noise or hot pixels dominate.
- Choose something smoother like Gaussian blur when soft denoising matters more than rejecting outliers.
- Choose bilateral filtering when you want smoothing plus stronger edge preservation on continuous images.
Key takeaways
- Median filtering is a rank filter, not a weighted average.
- It is especially strong against salt-and-pepper style corruption.
- It preserves edges better than a plain blur in many small-kernel settings.
- It is often a better pre-threshold cleanup step than Gaussian blur when the noise is impulsive.
Practice ideas
- Compare median and Gaussian filtering on the same image with isolated bright specks.
- Run thresholding after each filter and inspect which mask is cleaner.
- Vary the kernel size and watch when thin details start disappearing.
Relation to other topics
- Gaussian blur smooths by averaging; median filtering smooths by choosing the middle sample.
- Bilateral filtering also tries to preserve edges, but through weighted averaging rather than rank selection.
- Thresholding often benefits from a median pre-pass when the input contains isolated outliers.
Build on these first
These topics supply the mental model or preceding stage that this page assumes.
What this enables
Once the current operator feels natural, these are the most useful follow-up jumps.
Thresholding
Convert a grayscale image into a binary mask by splitting values into foreground and background.
Canny Edge Detection
Build a cleaner final edge map by chaining smoothing, gradients, non-maximum suppression, and hysteresis thresholding.
Related directions
These topics live nearby conceptually, even if they are not strict prerequisites.
Gaussian Blur
Smooth an image with a weighted local average so noise shrinks before thresholding, edge detection, or later analysis.
Bilateral Filter
Smooth an image while refusing to average across strong intensity jumps, making it a classic edge-aware denoiser.
Thresholding
Convert a grayscale image into a binary mask by splitting values into foreground and background.
More from Filtering & convolution
Stay in the same family when you want parallel operators built from the same mental model.
Gaussian Blur
Smooth an image with a weighted local average so noise shrinks before thresholding, edge detection, or later analysis.
Bilateral Filter
Smooth an image while refusing to average across strong intensity jumps, making it a classic edge-aware denoiser.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Filtering and edges
Move from raw pixels through noise reduction, edge emphasis, and multi-stage edge pipelines.
From the blog
Pair the graphics atlas with recent writing from the broader site whenever you want a wider engineering lens.