ArticleslgStudy

science

Median filter

Median filter is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Median filter rather than just read about it. In short: The median filter is a non-linear digital filtering technique, often used to remove noise from an image, signal, and video. Such noise reduction is a typical pre-processing step to improve the results of later processing (for example, edge detection on an image).

Median filter — main illustration
Median filter — illustration

Key takeaways

  • Median filter belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Median filter to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Median filter from memory before moving on to harder problems.

Reference excerpt

The median filter is a non-linear digital filtering technique, often used to remove noise from an image, signal, and video. Such noise reduction is a typical pre-processing step to improve the results of later processing (for example, edge detection on an image). Median filtering is very widely used in digital image processing because, under certain conditions, it preserves edges while removing noise (but see the discussion below for which kinds of noise), also having applications in signal processing.

Algorithm description The main idea of the median filter is to run through the signal entry by entry, replacing each entry with the median of the entry and its neighboring entries. The idea is very similar to a moving average filter, which replaces each entry with the arithmetic mean of the entry and its neighbors. The pattern of neighbors is called the "window", which slides, entry by entry, over the entire signal. For one-dimensional signals, the most obvious window is just the first few preceding and following entries, whereas for two-dimensional (or higher-dimensional) data, the window must include all entries within a given radius or ellipsoidal or rectangular region (i.e., the median filter is not a separable filter).

Worked one-dimensional example To demonstrate, using a window size of three with one entry immediately preceding and following each entry, and zero-padded boundaries, a median filter will be applied to the following simple one-dimensional signal:

x = (2, 3, 80, 6, 2, 3). This signal has mainly small valued entries, except for one entry that is unusually high and considered to be a noise spike, and the aim is to eliminate it. So, the median filtered output signal y will be:

y0 = med(0, 2, 3) = 2, (the boundary value is taken to be 0) y1 = med(2, 3, 80) = 3, (already 2, 3, and 80 are in the increasing order so no need to arrange them) y2 = med(3, 80, 6) = med(3, 6, 80) = 6, (3, 80, and 6 are rearranged to find the median) y3 = med(80, 6, 2) = med(2, 6, 80) = 6, y4 = med(6, 2, 3) = med(2, 3, 6) = 3, y5 = med(2, 3, 0) = med(0, 2, 3) = 2, i.e.,

y = (2, 3, 6, 6, 3, 2). It is clear that the noise spike has been essentially eliminated (and the signal has also been smoothed a bit). The result of a moving average filter with the same window width on the same dataset would be y = (1.7, 28.3, 29.7, 29.3, 3.7, 1.7). It can be seen that the noise spike has infected neighbouring elements in the moving average signal, and that the median filter has performed much better (for this type of impulse noise). Median filtering works well for both positive impulses (spikes) and negative impulses (dropouts), so long as a window can be chosen so that the number of entries infected with impulse noise is (almost) always smaller than half of the window size.

Boundary issues When implementing a median filter, the boundaries of the signal must be handled with special care, as there are not enough entries to fill an entire window. There are several schemes that have different properties that might be preferred in particular circumstances:

When calculating the median of a value near the boundary, missing values are filled by repeating the boundary value to obtain enough entries to fill the window. Avoid processing the boundaries, with or without cropping the signal or image boundary afterwards, Fetching entries from other places in the signal such as values from the far ends (repeating boundary conditions) or reversing the signal (reflected boundary conditions). With 2D images for example, entries from the far horizontal or vertical boundary might be selected, or repeating in reverse order the points at the same boundary Shrinking the window near the boundaries, so that every window is full, Assuming zero-padded boundaries.

Two-dimensional median filter pseudo code Code for a simple two-dimensional median filter algorithm might look like this:

1. allocate outputPixelValue[image width][image height] 2. allocate window[window width × window height] 3. edgex := (window width / 2) rounded down 4. edgey := (window height / 2) rounded down

for x from edgex to image width - edgex do for y from edgey to image height - edgey do i = 0 for fx from 0 to window width do for fy from 0 to window height do window[i] := inputPixelValue[x + fx - edgex][y + fy - edgey] i := i + 1 sort entries in window[] outputPixelValue[x][y] := window[window width * window height / 2]

This algorithm:

Processes one color channel only, Takes the "not processing boundaries" approach (see above discussion about boundary issues).

Algorithm implementation issues By far the majority of the computational effort and time is spent on calculating the median of each window. Because the filter must process every entry in the signal, for large signals such as images, the efficiency of this median calculation is a critical factor in determining how fast the algorithm can run. The naïve implementation described above sorts every entry in the window to find the median; however, since only the middle value in a list of numbers is required, selection algorithms can be much more efficient. Furthermore, some types of signals (very often the case for images) use whole number representations: in these cases, histogram medians can be far more efficient because it is simple to update the histogram from window to window, and finding the median of a histogram is not particularly onerous.

Worked two-dimensional example The median filter operates by considering a local window (also known as a kernel) around each pixel in the image. The steps for applying the median filter are as follows:

… excerpt ends here. Continue reading the full article.

Illustrations

Median filter: Example of 3 median filters of varying radiuses applied to the same noisy photograph.
Example of 3 median filters of varying radiuses applied to the same noisy photograph.
Median filter: Use of a median filter to improve an image severely corrupted by defective pixels
Use of a median filter to improve an image severely corrupted by defective pixels

Worked examples

Example 1 — a first encounter with Median filter

Start with the simplest possible case. Write down what Median filter claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Median filter before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Median filter ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Median filter

In research
Median filter appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Median filter in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Median filter is common in secondary-school and first-year university syllabi. It links to neighbouring topics Image noise reduction techniques, Nonlinear filters, Signal processing, so understanding it makes those chapters shorter.
In everyday life
Look for Median filter outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Median filter in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Median filter means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Median filter out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Median filter in simple terms?

The median filter is a non-linear digital filtering technique, often used to remove noise from an image, signal, and video. Such noise reduction is a typical pre-processing step to improve the results of later processing (for example, edge detection on an image).

Why does Median filter matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Median filter?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Median filter.

Tags

  • Image noise reduction techniques
  • Nonlinear filters
  • Signal processing

Keep exploring