Using a median filter to reduce noise on an image with python.

This year I've been very busy with a lot of stuff at my job and homework from my master's degree. Recently, I had to do an activity that I really liked, and I wanted to share it on my personal blog.

We were experimenting with different kinds of filters for images to achieve noise reduction; there is a wide range of filters and tools designed for this purpose, but this time I'm focusing on a very simple filter called the "median" filter.

The median filter, as the name implies, makes use of a very known mathematical tool with the same name in order to decide which value applies to the pixel is going to filter.

What is the Median Filter?

The median filter, as the name implies, makes use of a well-known mathematical tool—the median—to decide which value to apply when filtering a pixel.

Back to the basics...

I already started to talk about filters and calculate the median but let's go back a minute and remember very quick how we can treat images; Images at a very basic level, are just big pixel 2x2 arrays, that can be treated as 1-dimensional arrays too if we want, just like a pixel stream. 


I already started to talk about filters and calculating the median, but let's go back for a minute and quickly remember how we can treat images. At a very basic level, images are just large 2D arrays of pixels, which can also be treated as 1-dimensional arrays if needed, just like a pixel stream.

Imagine that we have an image of 384px × 256px; that would be the size of our 2D array. When we apply a filter to our image, we need to iterate through all the pixels of our original image and apply a certain transformation or mathematical operation to achieve the desired effect.

How Does the Median Work?

The median is a simple yet powerful tool that you might remember from your college years. If not, here’s a quick refresher. To obtain the median from a set of data, follow these three steps:

  1. Order the set of values in ascending order.

  2. If the number of values is odd, the median is the middle value.

  3. If the number of values is even, take the two middle values, sum them, and divide by 2 to obtain the median.


Simple right? 

Applying the Median Filter

Now, how do we filter our image using this median tool? Well, for that, we need what is called a "kernel" or a "mask." This is essentially another 2D array of an odd-numbered size like 3×3, 5×5, etc., ensuring that there is always a center pixel. For example, in a 3×3 kernel, we will have 9 coefficients:

But how we can filter our image using this "median" tool?... well, for that, we need what is called a "kernel" or a "mask", which basically is another 2d array that is needed to be an odd number size like 3x3 or 5x5 etc, so it always has a center pixel and for example, for a 3x3 kernel we will have 9 coefficients like the image below:

The center pixel (marked as o) will be replaced by the median of the surrounding values.

Implementing the Median Filter in Python

Now, let’s jump into the code! We will use OpenCV and NumPy to apply a median filter on an image. First, install OpenCV if you haven’t already:

"!pip install opencv-python numpy"

Now we write the next code: 


Here is the result after applying the median filter, of course, we lose some details but this is a very basic filter that along with other tools has a lot of potential and shows us the basics of denoising filtering.





How Does This Work?

  1. We load an image in grayscale.

  2. We apply the cv2.medianBlur() function, which replaces each pixel’s value with the median of its surrounding pixels.

  3. Finally, we use matplotlib to visualize the original and filtered images side by side.

Why Use a Median Filter?

Great for noise reduction: It effectively removes salt-and-pepper noise (random black and white pixels) without blurring edges as much as an average filter would. ✅ Simple and fast: Requires only sorting pixel values within a small kernel window. ✅ Works well on grayscale images: Can also be extended to color images by applying it to each channel separately.

Final Thoughts

The median filter is a powerful yet simple way to reduce noise in images while preserving edges. It's commonly used in preprocessing steps before further image analysis or computer vision tasks.

Try it out with your own images and experiment with different kernel sizes to see how the filtering effect changes!

Comentarios

Entradas populares