Hi there, today we will be talking about something interesting as well as simple : How to Blur an Image Using OpenCV module in Python. An image has many features (for example:- edge, contrast etc.). A blurred image is really smooth that means the edges are not observed here. Blurring an image has many benefits (for example: it removes noise and low intensity edges, it helps to hide the details). We can blur any image using a few lines of codes.
Let’s get started.
Blurring an Image Project
Requirements:
Basic knowledge of python(go through Learn Python Tutorial) . We can use any ide ( for example: https://code.visualstudio.com/download , https://jupyter.org/).
Modules:
OpenCV : It is a python open-source library, we use it for all sorts of image and video analysis, for instance, facial recognition and detection, photo editing etc.
NumPy : NumPy (an acronym for “Numeric Python” or “Numerical Python”) is a library for the Python programming language. It is used for working with large ,multi-dimensional arrays and matrices.
Installation:
Firstly, we have to open command prompt in administrator mode.
OpenCV: To install OpenCV we have to type the below command in the terminal.
pip install opencv-python
NumPy :To install Numpy we will type the below command in the terminal.
pip install numpy
Approach (for gaussian blur):
Gaussian blurring is widely used in machine learning and deep learning models and graphics software.
1. Firstly, we will import the module.
import cv2 import numpy as np
2. Secondly, we will read the selected image using OpenCV.
MyImage = cv2.imread ( )
3. Thirdly, we will use the gaussian function for blurring the image, then will show the image and finally we have to destroy all the windows.
Gaussian = cv2.GaussianBlur(MyImage, (7, 7), 0) cv2.imshow('Gaussian Blurring', Gaussian) cv2.waitKey(0) cv2.destroyAllWindows( )
Approach (for median blur):
Median blurring is widely used in digital image processing.
1. Firstly, we will import the module.
import cv2 import numpy as np
2. Secondly, we will read the selected image using OpenCV.
MyImage = cv2.imread ( )
3. Thirdly, we will use the median function for blurring the image, then will show the image and finally we have to destroy all the windows.
median = cv2.medianBlur(MyImage, 5) cv2.imshow('Median Blurring', median) cv2.waitKey(0) cv2.destroyAllWindows( )
Approach (for bilateral blur):
Using this blurring filter we can preserve the sharp edges while the weak ones will be discarded.
1. Firstly, we will import the module.
import cv2 import numpy as np
2. Secondly, we have to read the selected image using OpenCV.
MyImage = cv2.imread ( )
3. Thirdly, we will use the function for blurring the image, then will show the image and finally we have to destroy all the windows.
bilateral = cv2.bilateralFilter(MyImage, 9, 75, 75) cv2.imshow('Bilateral Blurring', bilateral) cv2.waitKey(0) cv2.destroyAllWindows( )
Source Code(combined) :
import cv2 import numpy as np MyImage = cv2.imread("path_name_of_the_image_file") cv2.imshow('Original Image', MyImage) cv2.waitKey(0) Gaussian = cv2.GaussianBlur(MyImage, (7, 7), 0) cv2.imshow('Gaussian Blurring', Gaussian) cv2.waitKey(0) median = cv2.medianBlur(MyImage, 5) cv2.imshow('Median Blurring', median) cv2.waitKey(0) bilateral = cv2.bilateralFilter(MyImage, 9, 75, 75) cv2.imshow('Bilateral Blurring', bilateral) cv2.waitKey(0) cv2.destroyAllWindows()
Hope, you liked this tutorial.