Table of Contents
In this tutorial, we will see how to Blurring an image in python programming language using open-cv, which exists as a cv2 (computer vision) library in python.
You can use GaussianBlur()
method of cv2
library to blur an image. In order to use cv2 library, you need to import cv2 library using import statement
. You can read about more about Gaussian function
The blurring of an image means smoothening of an image i.e., removing outlier pixels that may be noise in the image.
Now let’s see the syntax and return value of GaussianBlur()
method, then we will move on the examples.
Syntax
1 2 3 |
cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType) |
Parameters
src:
Source/Input of n-dimensional array.ksize:
Kernal is matrix of an (no. of rows)*(no. of columns) order .Its Size is given in the form of tuple (no. of rows, no. of columns). no. of rows and no. of columns should be odd .If ksize is given as (0 0), then ksize is computed from given sigma values i.e. sigmaX and sigmaY.sigmaX:
Standard deviation value of kernal along horizontal direction.sigmaY:
Standard deviation value of kernal along vertical direction.borderType:
This specify boundaries of an image while kernel is applied on borders of an image.- cv2.BORDER_CONSTANT
- cv2.BORDER_REPLICATE
- cv2.BORDER_REFLECT
- cv2.BORDER_WRAP
- cv2.BORDER_REFLECT_101
- cv2.BORDER_TRANSPARENT
- cv2.BORDER_REFLECT101
- cv2.BORDER_DEFAULT
- cv2.BORDER_ISOLATED
Possible values of borderType are :
Return Value
It returns Output blurred image of n-dimensional array.
a) In
GaussianBlur()
method, you need to passsrc
andksize
values everytime and either one, two, or all parameters value from remainingsigmax
,sigmaY
andborderType
parameter should be passed.b) Both sigmaX and sigmaY parameters become optional if you mention the
ksize(kernal size)
value other than(0,0)
.
cv2 GaussianBlur() Method example
Now Let’s see the Python code :
Example 1:
Using GaussianBlur() method with src
,ksize
and borderType
parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # Load/Read an image image = cv2.imread(img_path) # show the image on the newly created image window cv2.imshow('Input image',image) # applying gaussian blur on the image blur_img = cv2.GaussianBlur(image,(5,5),cv2.BORDER_DEFAULT) # show the image on the newly created image window cv2.imshow('Blur image',blur_img) |
Output :
Example 2:
Using GaussianBlur()
method with src
,ksize
and sigmaX
parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# import computer vision library(cv2) in this code import cv2 # main code if __name__ == "__main__" : # mentioning absolute path of the image img_path = "C:\\Users\\user\\Desktop\\flower.jpg" # Load/Read an image image = cv2.imread(img_path) # show the image on the newly created image window cv2.imshow('Input image',image) # applying gaussian blur on the image with kernal size(5,5) # and sigmaX = 5 blur_img = cv2.GaussianBlur(image,(5,5),5) # show the image on the newly created image window cv2.imshow('Blur image',blur_img) |
Output :
You may similarly change the values of other properties and observe the outputs.
That’s all about cv2 GaussianBlur() Method in Python.