cv2 Gaussianblur : Blur an Image in Python using OpenCV

There are many functions in OpenCV that allow you to manipulate your input image. cv2.Gaussianblur() is one of them. It allows you to blur images that are very helpful while processing your images. In this entire tutorial you will know how to blur an image using the OpenCV python module.

 

Why We blur the image?

Just like preprocessing is required before making any machine learning model. In the same way, removing noise in the image is important for further processing of the image. Gaussian Blurring the image makes any image smooth and remove the noises. In the next section, you will know all the steps to do the Gaussian blur using the cv2 Gaussianblur method.

Steps to Blur the image in Python using cv2.Gaussianblur()

Step 1: Import all the required libraries

In the entire tutorial, I am using two libraries. One is OpenCV and another is matplotlib. The latter will be used for displaying the image in the Jupyter notebook.

import cv2
import matplotlib.pyplot as plt
%matplotlib inline

In our tutorial, I am displaying all the images inline. That’s why I am telling the python interpreter to display images inline using %matplotlib inline.

Step 2: Read the image file

Before blurring the image you have to first read the image. In OpenCV, you can read the image using the cv2.imread() method. Let’s read the image.

# read image
img = cv2.imread("owl.jpg")
plt.imshow(img)

Output

Sample image for implementing cv2.Gaussianblur()

You can see the original image is not blurred. In the next step, I will perform the Gaussian Blur on the image.

Step 3: Blur the image using the cv2.Gaussianblur  method

Before applying the method first learns the syntax of the method. The cv2.Gaussianblur() method accepts the two main parameters. The first parameter will be the image and the second parameter will the kernel size. The OpenCV python module use kernel to blur the image. And kernel tells how much the given pixel value should be changed to blur the image. For example, I am using the width of 5 and a height of 55 to generate the blurred image. You can read more about it on Blur Documentation.

Execute the below lines of code and see the output.

blur = cv2.GaussianBlur(img,(5,55),0)
plt.imshow(blur)

Output

Gaussian Blurred Owl Image

Now You can see the blurred image.

These are the steps to perform Gaussian Blur on an image. Hope you have loved this article. If you have any queries then you can contact us for getting more help.

Other CV2 Utilities :

How to use cv2.imshow in python : Know it with Examples

cv2 imread method implementation in Python ( Size, Shape, Type, Length )

How to Resize an Image using cv2.resize() method: 3 Steps Only

Thanks
Data Science Learner Team