Smoothing/Blurring Images In Python
This tutorial will help you learn how to convert a real image into a smooth one. In this tutorial, you will also learn how to install the important image processing library OpenCV(open computer vision).
What Is Smoothing Of Image?
Smoothing of the image is nothing but reducing noise in the image to use for some purpose like shape detection. It’s also known as the blurring of the image.
Several methods are used for smoothing images in Python, which we used to make our images are mentioned below :
- BoxFilter/ Averaging
- Median Filtering
- Gaussian Filtering
- Bilateral Filtering
We will use all these four techniques in our program. These methods belong to OpenCV library which is the image or video processing library in Python.
For the installation, you can take the first part of this tutorial: OpenCV gradient tutorial
Python Program For Smoothing Of Image
import cv2 import numpy as np import matplotlib.pyplot as plot image_src =cv2.imread("logo_og.png", -1) image_src=cv2.cvtColor(image_src,cv2.COLOR_BGR2RGB)
From line no. 7 to 15 are methods to convert real image into 5 different blurred images. Line no. 7 calculates kernel by a 5*5 matrix fill with 1 of float32 data type and divided by 25( which is row*coloumn). box_filter() takes argument image source first, next is a depth and the third one is the kernel( which we calculated in 7). Other methods are gaussian, bilateral and median which also take arguments matrix and image source.
kernel = np.ones((5,5),np.float32)/25 box_filter = cv2.filter2D(image_src,-1,kernel) blur =cv2.blur(image_src,(5,5)) gaussian =cv2.GaussianBlur(image_src,(5,5),0) median = cv2.medianBlur(image_src,0) bilateral = cv2.bilateralFilter(image_src,9,75,75)
and the below code helps you to plot images on your screen. How this code works just go on this link. Click here.
titles =["REAL","BOX","BLUR","MEDIAN","GAUSSIAN" ,"BILATERAL"] results =[image_src,box_filter,blur,median,gaussian,bilateral] for i in range(6): plot.title(titles[i-1]) plot.subplot(2,3,i+1) plot.imshow(results[i],"flag") plot.xticks([]) plot.yticks([]) plot.show()
Input :
Output :
Leave a Reply