Hello everybody,
Michael here, and in this post, we’ll explore image blurring! Image blurring is a pretty self-explanatory process since the whole point of image blurring is to make the image, well, blurry. This process has many uses, such as blurring the background on your work video calls (and yes, I do that all the time during work video calls).
Intro to image blurring
Now that we know a little bit about image blurring, let’s explore it with code. Here’s the image that we’ll be using:

The photo above is of Stafford Park, a lovely municipal park in Miami Springs, FL.
Unlike image eroding, image blurring has a pretty self-explanatory description since the aim of this process is to, well, blur images. How can we accomplish this through OpenCV?
Before we get into the fun image-blurring code, let’s discuss the three main types of image blurring that are possible with OpenCV:
- Gaussian blur-this process softens out any sharp edges in the image
- Median blur-this process helps remove image noise* by changing pixel colors wherever necessary
- Bilateral blur-this process makes the central part of the image clearer while making any non-central part of the image fuzzier
*For those unfamiliar with image processing, image noise is (usually unwanted) random brightness or color deviations that appear in an image. Median blurring assists you with removing image noise.
Now that we know the three different types of image blurring, let’s see them in action with the code
Gaussian blur
Before we start to blur the image, let’s read in the image in RGB colorscale:
import cv2
import matplotlib.pyplot as plt
park=cv2.imread(r'C:\Users\mof39\OneDrive\Documents\20230629_142648.jpg', cv2.IMREAD_COLOR)
park=cv2.cvtColor(park, cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.imshow(park)

Next, let’s perform a Gaussian blur of the image:
gaussianPark = cv2.GaussianBlur(park, (7, 7), 10, 10)
plt.figure(figsize=(10,10))
plt.imshow(gaussianPark)

Notice anything different about this image? The sharp corners in this photo (such as the sign lettering) have been smoothed out, which is the point of Gaussian blurring (to smooth out rough edges in an image).
Now, what parameters does the cv2.GaussianBlur() method take?
- The image you wish to blur (
parkin this case) - A 2-integer tuple indicating the size of the kernel you wish to use for the blurring process-yes, this is similar to the kernels we used for image erosion in the previous post Python Lesson 45: Image Resizing and Eroding (AI pt. 11) (we’re using a 7-by-7 kernel here).
- Two integers that represent the
sigmaXandsigmaYof the Gaussian blur that you wish to perform. What aresigmaXandsigmaY? Both integers represent the numerical factors you wish to use for the image blurring-sigmaXbeing the factor for horizontal blurring andsigmaYbeing the factor for vertical blurring.
A few things to keep in mind regarding the Gaussian blurring process:
- Just as you did with image erosion, ensure that both dimension of the blurring kernel are positive and odd-numbered integers (like the 7-by-7 kernel we used above).
sigmaXandsigmaYare optional parameters, but keep in mind if you don’t include a value for either of them, both will default to a 0 value, which might not blur your picture the way you intended. Likewise, if you use a very high value for both sigmas, you’ll end up with a very, very blurry picture.
Median blur
Since median blurring helps remove image noise, we’re going to be using this altered park image with a bunch of noise for our demo:

Next up, let’s explore the median blur with our noisyPark image:
medianPark = cv2.medianBlur(noisyPark, 5)
plt.figure(figsize=(10,10))
plt.imshow(medianPark)

As you can see, median blurring the noisyPark image cleared out a significant chunk of the image noise! But how does this function work? Let’s explore some of its parameters:
- The image you wish to blur (
noisyParkin this case) - A single integer indicating the size of the kernel you wish to use for the blurring process-yes, this is similar to the kernels we used for Gaussian blurring, but you only need a single integer instead of a 2-integer tuple (we’re using a 5-by-5 kernel here). The integer must be a positive and odd number since the kernel must be an odd number (same rules as the Gaussian blur apply here for kernel creation).
Bilateral blur
Last but not least, let’s explore bilateral blurring! This time, let’s use the non-noise altered park image.
bilateralPark = cv2.bilateralFilter(park, 12, 120, 120)
plt.figure(figsize=(10,10))
plt.imshow(bilateralPark)

Wow! As I mentioned earlier, the purpose of bilteral blurring is to make a central part of the image clearer while make other, non-central elements of the image blurrier. And boy, does that seem to be the case here since the central element of the image (the park sign and all its lettering) really pops out while everything in the background seems a bit blurrier.
How does the cv2.bilateralFilter() function work its magic? Here’s how:
- The image you wish to blur (
parkin this case) - The diameter (in pixels) of the region you wish to iterate through to blur-in this case, I chose a 12-pixel diameter as my “blurring region”. It works in a similar fashion to the kernels we used for our “erosion region” in the previous lesson.
- The next two integers-both 120-are the
sigmaColorandsigmaSpacevariables, respectively. ThesigmaColorvariable is a factor that considers how much color should be considered in the blurring process while thesigmaSpacevariable is a factor that considers the proximity of several elements in the image (such as the runners in the background). The higher both of these values are, the blurrier the background will be.
Thanks for reading,
Michael