Python | Blurring an image using OpenCV



Blur benefits:

  • It helps in removing noise. As noise is considered as a high-pass signal, so when applying a low-pass filter kernel we limit the noise.
  • This helps in smoothing the image.
  • Low intensity edges are removed.
  • This helps to hide details when needed. For example, in many cases the police intentionally want to hide the victim`s face, in such cases blurring is required.

Important types of blur:

  • Gaussian Blur: Gaussian Blur is the result of blurring an image using the Gaussian function. It is a widely used effect in graphics software, usually to reduce image noise and detail. It is also used as a preprocessing stage before applying our machine learning or deep learning models. 
    For example, Gaussian kernel (3 × 3)
  • Blur on median: Median filter — it is a nonlinear digital filtering technique often used to remove noise from an image or signal. Median filtering is very widely used in digital image processing because, under certain conditions, it preserves edges while removing noise. It is one of the best algorithms for removing salt and pepper.
  • Two-sided blur: Two-sided filter — it is a non-linear, edge-preserving and noise-reducing anti-aliasing filter for images. It replaces the intensity of each pixel with a weighted average of the intensity from neighboring pixels. This weight can be based on a Gaussian distribution. This way, sharp edges are preserved while discarding weak ones.

Below is the Python code:

# import libraries

import cv2

import numpy as np

 

image = cv2.imread ( ` C: //Geeksforgeeks//image_processing//fruits.jpg ` )

 

cv2.imshow ( `Original Image ` , image)

cv2.waitKey ( 0 )

  
# Gaussian Blur

Gaussian = cv2.GaussianBlur (image, ( 7 , 7 ), 0 )

cv2.imshow ( `Gaussian Blurring` , Gaussian)

cv2.waitKey ( 0 )

 
# Median Blur

median = cv2.medianBlur (image, 5 )

cv2.imshow ( `Median Blurring`  , median)

cv2.waitKey ( 0 )

 

 
# Bilateral blur

bilateral = cv2.bilateralFilter (image, 9 , 75 , 75 )

cv2.imshow ( `Bilateral Blurring` , bilateral)

cv2.waitKey ( 0 )

cv2.destroyAllWindows ()

Output: