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.
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.
Applying this low-pass filter smoothens the sharp edges in the image which gives it a blurred look.
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
For a
In terms of convolution, applying this filter would mean adding the values of nine pixels and then dividing by
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.
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
The gray boxes represent the zero-padding and the
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.requestfrom PIL import Image, ImageFilterurl = "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")
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