How to blur an image using a mean filter

Blurring is used as a preprocessing step in many computer vision applications since it can remove noise (high-frequency components). Photographers also blur some parts of the image so that the main object can be more prominent than the background.

Original image
Blurred image

Blurring an image is the process of applying a low-pass filter. A low-pass filter allows low-frequency components and blocks the components with high frequency.

In terms of images, the high-frequency components are those where we see an abrupt change in the pixel values. Similarly, low-frequency components show a gradual change in the pixel values.

Low-frequency components
High-frequency components

Applying this low-pass filter smoothens the sharp edges in the image which gives it a blurred look.

Mean filter

Mean filtering is also known as averaging, box filtering, and smoothing. Mean filtering takes the average of pixel values in the neighboring pixels and replaces this pixel value with the average. Like most image filtering techniques, it uses the convolutionConvolution is a mathematical operation where a function is applied to another function to result in a mixture of the two functions. operation to apply the filter.

For a 3×33 \times 3 filter, the mean filter, more commonly known as a kernel, is as follows:

A 3x3 mean filter

In terms of convolution, applying this filter would mean adding the values of nine pixels and then dividing by 99. This gives us the average of these nine pixels.

Note: For a larger kernel size, a greater blur effect will be observed. This will reduce more noise. However, noise reduction is inversely proportional to image quality. More noise reduction will result in losing important information from the image as well.

Visualization

In this section, we will visualize the process of applying a mean filter to an image. For simplicity, we assume that our image is of 3×33 \times 3 dimensions and the filter is the same as above. Usually, the image is padded with zeros as shown below:

An image convolution operation using mean filter

The gray boxes represent the zero-padding and the * is the convolution operator. This will be calculated as follows:

1 of 9

Code

In this section, we will apply the mean filter to the unblurred image shown above. We'll use the Pillow library to apply the mean filter to the image above.

import urllib.request
from PIL import Image, ImageFilter
url = "https://github.com/nabeelraza-7/images-for-articles/blob/main/educative.jpeg?raw=true"
filename = "image.jpeg"
urllib.request.urlretrieve(url, filename)
img = Image.open(filename)
blurred = img.filter(ImageFilter.BoxBlur(radius=4))
blurred.save("output/blurred.jpeg")

Explanation

  • Line 8: Here, we have used the built-in urllib.request to get the image from the web. This is stored in the local directory with the given name.

  • Line 9: Here, we use the same file that we have downloaded from the web and opened it using Image.open().

  • Line 10: Here, we use BoxFilter() defined in ImageFilter. It has one required parameter, radius. This radius defines how many neighbors will be used in the kernel.

Note: To get the filter above, we would set the radius to 1. However, this will be negligible to observe.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved