Sunday, December 29, 2024
Google search engine
HomeLanguagesOpenCV Python Program to blur an image

OpenCV Python Program to blur an image

Note: This post contains codes that cannot be run using an online compiler. Please make sure that you have Python 2.7 and cv2 module installed before trying to run the program on your system.

Hi everyone! I read a brilliant work by Aditya PrakashOpenCV C++ Program to blur an image, so I decided to come up with something similar but this time in Python. So, here is a very simple program with basically the same result.




# Python Program to blur image
  
# Importing cv2 module
import cv2 
  
# bat.jpg is the batman image.
img = cv2.imread('bat.jpg'
  
# make sure that you have saved it in the same folder
# You can change the kernel size as you want
blurImg = cv2.blur(img,(10,10)) 
cv2.imshow('blurred image',blurImg)
  
cv2.waitKey(0)
cv2.destroyAllWindows()


Output:

Now, this program above is using image blurring technique called Averaging.There are some other options available as well – Gaussian Blurring, Median Blurring, Bilateral Filtering. Let’s make a couple of additions in our program and compare the results.




# importing opencv CV2 module
import cv2 
  
# bat.jpg is the batman image.
img = cv2.imread('gfg.png')
   
# make sure that you have saved it in the same folder
# Averaging
# You can change the kernel size as you want
avging = cv2.blur(img,(10,10))
   
cv2.imshow('Averaging',avging)
cv2.waitKey(0)
  
# Gaussian Blurring
# Again, you can change the kernel size
gausBlur = cv2.GaussianBlur(img, (5,5),0
cv2.imshow('Gaussian Blurring', gausBlur)
cv2.waitKey(0)
  
# Median blurring
medBlur = cv2.medianBlur(img,5)
cv2.imshow('Media Blurring', medBlur)
cv2.waitKey(0)
  
# Bilateral Filtering
bilFilter = cv2.bilateralFilter(img,9,75,75)
cv2.imshow('Bilateral Filtering', bilFilter)
cv2.waitKey(0)
cv2.destroyAllWindows()


Original Image:

Averaging:

Gaussian Blurring:

Media Blurring:

Bilateral Filtering:

Hope you enjoyed the post! Auf Wiedersehen!

About the author:

Vishwesh Shrimali is an Undergraduate Mechanical Engineering student at BITS Pilani. He fulfils about all the requirements not taught in his branch- white hat hacker, network security operator, and an ex – Competitive Programmer. As a firm believer in power of Python, his majority work has been in the same language. Whenever he get some time apart from programming, attending classes, watching CSI Cyber, he go for a long walk and play guitar in silence. His motto of life is – “Enjoy your life, ‘cause it’s worth enjoying!”

If you also wish to showcase your blog here, please see GBlog for guest blog writing on Lazyroar.

RELATED ARTICLES

Most Popular

Recent Comments