AI Glossary / Computer vision and multimodal AI / Beginner

Median filter

Definition

A median filter replaces each value with the median of values in a nearby window. In image processing, it is a nonlinear neighborhood filter often used to reduce isolated bright or dark disturbances. Its behavior depends on the window shape, size, boundary handling, and the data being filtered.

What it means

The median is the middle value after sorting an odd number of observations. Unlike an average, it is not pulled directly toward an extreme value merely because that value becomes much larger or smaller.

This makes median filtering useful for some forms of impulse noise, often called salt-and-pepper noise in images. A single extreme pixel surrounded by ordinary values may be replaced by a value more consistent with its neighbors.

The filter does not know which pixels are noise. A tiny genuine feature can also be outnumbered by surrounding pixels and disappear. Preserving edges better than a simple average in some situations does not mean preserving every edge, corner, or narrow structure unchanged.

How it works

Choose a neighborhood, such as a three-by-three square around each grayscale pixel. Collect its values, determine the median, and write that value to the corresponding output location. Conceptually each output uses the original input neighborhood rather than previously updated output values.

Repeat across the image using a defined rule where the window extends beyond its boundaries. The footprint can be shaped for the application, and implementations may support several dimensions or selected axes.

For an odd number of scalar samples, the median is one of the sampled values. Even-sized neighborhoods need an explicit convention: some implementations average two central values, while others select a particular ranked element. Color images also require care, because processing channels separately is different from selecting a vector median in color space.

Three practical examples

1. Document cleanup: A scanned-page workflow tests a small median filter to reduce isolated speckles. It verifies that punctuation and thin character strokes remain readable before applying the filter broadly.

2. Sensor-image preprocessing: An inspection system evaluates median filtering for occasional extreme pixels. It compares the result with known defect examples so genuine small spots are not removed accidentally.

3. Mask cleanup: A developer applies a median-like neighborhood rule to a binary mask to suppress isolated classifications. The team checks whether the operation closes meaningful holes or disconnects narrow regions.

Think of it this way

Think of choosing the middle opinion from a small ordered group rather than calculating the group's average. One wildly extreme opinion may leave that middle choice unchanged.

The analogy stops at numerical ordering. Nearby image values do not vote on whether a feature is real, and a minority of pixels can represent exactly the detail an application needs to preserve.

Worked example

Consider a three-by-three grayscale neighborhood with values 98, 100, 101, 99, 255, 102, 100, 97, and 99. Sorted, they become 97, 98, 99, 99, 100, 100, 101, 102, 255. The fifth value is 100, so the median-filter output is 100.

The arithmetic mean would be 1051/9, approximately 116.78. The extreme value 255 raises that average, while it does not change the median beyond its rank position. If 255 were replaced by 1000 in a suitable numeric representation, the median would still be 100.

Now take a binary three-by-three neighborhood containing a one-pixel-wide vertical line: three foreground ones and six background zeros. The median is zero. A genuine thin line can therefore vanish under this window, even though the operation is working exactly as defined.

These calculations explain both the benefit and the risk. The same resistance to isolated values can suppress noise or erase meaningful sparse structure.

Illustrative case study

Fictional case: An archive team cleans old scanned invoices with a median filter. Dust specks become less visible, but reviewers notice that some decimal points and small punctuation marks have also disappeared.

The team compares original and processed text regions at readable scale and evaluates smaller or more targeted cleanup. It retains the original scans and treats the filtered images as derived versions, so details can be checked when interpretation matters.

The scenario illustrates a task-specific tradeoff. It does not claim that median filtering always harms document recognition or prescribe one setting for every archive.

Common misconceptions and mistakes

Do not confuse the median with the mean. Sorting and choosing a middle-ranked value behaves differently from summing and dividing.

Do not describe the filter as a fixed linear convolution. In general, filtering a sum is not the same as adding separately median-filtered inputs, so linear superposition does not apply.

Avoid treating all noise types as equally suitable for median filtering. Also check data axes: a general multidimensional filter may include the channel dimension unless configured otherwise. Boundary rules, missing values, and even-sized footprints can change results, so document them when reproducibility matters.

Business use

Median filtering offers a simple, interpretable preprocessing option for particular local disturbances. It can support image cleanup, segmentation, and measurement workflows when its footprint matches the scale of unwanted features.

Evaluate the smallest important real structures alongside noisy regions. Measure downstream recognition or inspection performance rather than relying only on a cleaner-looking preview. Keep the window and boundary settings with the processing record. If image resolution changes, revisit the settings because the same pixel window then covers a different physical area.

People also ask

Does the filter always output an existing neighborhood value?

For an odd-sized scalar neighborhood, yes. Even-sized conventions and multichannel variants can differ.

Is a larger window always better at cleaning noise?

It can suppress more variation, but it can also remove larger genuine features and alter boundaries.

Can median filtering sharpen an image?

Its main role is not sharpening. It may leave some edges cleaner than averaging would, but it does not recover missing spatial detail.

Check your understanding

A small dark punctuation mark disappears after median filtering. Does that necessarily indicate an implementation bug?

Answer: No. If background values dominate its neighborhood, the defined median can replace the mark with background.

Related terms

  • Median
  • Salt-and-pepper noise
  • Nonlinear filter
  • Neighborhood
  • Order statistic
  • impulse noise
  • mathematical morphology
  • image denoising

Learn next

Compare mean and median filtering on an isolated spike, a sharp edge, and a thin line. Then repeat with a different window size and inspect both noise removal and loss of meaningful structure.

  • Median

More in Computer vision and multimodal AI

  • Maze / labyrinth artifacts - Maze or labyrinth artifacts are spurious, maze-like patterns that can appear in reconstructed digital images, particularly after demosaicking raw sensor data.
  • Mean field approximation - Mean field approximation simplifies a system of interacting variables by representing their combined behavior through separate factors and average effects.
  • Medial axis transform - The medial axis transform represents a shape through its central skeletal structure together with distances to its boundary.
  • Median-threshold bitmap (MTB) - A median-threshold bitmap (MTB) is a binary image that marks whether each pixel is brighter than the image's median intensity.
  • Membrane - In Poisson image editing, a membrane is a smooth correction surface that carries the difference between source and destination boundary values into the...
  • Merit function - A merit function is a numerical objective used to score candidate solutions during optimization.

Sources

SciPy, median_filter official documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.median_filter.html

OpenCV, Smoothing Images, median-blurring section: https://docs.opencv.org/4.12.0/d4/d13/tutorial_py_filtering.html

Both official references were opened. The neighborhood calculations and archive scenario are original examples; even-sized and multichannel behavior is explicitly implementation-dependent.

Previous: Median absolute error Next: Median test

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer