Change language

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:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically