OpenCV Python Program to blur an image?

PythonServer Side ProgrammingProgramming

OpenCV is one of the best python package for image processing. Also like signals carry noise attached to it, images too contain different types of noise mainly from the source itself (Camera sensor). Python OpenCV package provides ways for image smoothing also called blurring. This is what we are going to do in this section. One of the common technique is using Gaussian filter (Gf) for image blurring. With this, any sharp edges in images are smoothed while minimizing too much blurring.

Syntax

cv.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType=BORDER_DEFAULT]]] )

Where−

  • src – input image

  • dst – output image

  • ksize – Gaussian kernel size[ height width]. If ksize is set to [0 0], then ksize is computed from sigma values.

  • sigmaX – Kernel standard deviation along X-axis(horizontal direction).

  • sigmaY – kernel standard deviation along Y-axis(Vertical direction).

  • Bordertype – Specifies iage boundaries while kernel is applied on image borders. Few possible values are: cv.BORDER_CONSTANT, cv.BORDER_REPLICATE, cv.BORDER_REFLECT, cv.BORDER_WRAP, cv.BORDER_DEFAULT, cv.BORDER_ISOLATED, cv.BORDER_TRANSPARENT etc.

Below is the program to Gaussian blur an image using OpenCV package.

import cv2
import numpy

# read image
src = cv2.imread('LionKing.jpeg', cv2.IMREAD_UNCHANGED)

# apply guassian blur on src image
dst = cv2.GaussianBlur(src,(3,3),cv2.BORDER_DEFAULT)

# display input and output image
cv2.imshow("Gaussian Blur",numpy.hstack((src, dst)))
cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

Result

The two images looks almost similar (original/blur). Now let us increase the kernel size and observe the result.

dst = cv2.GaussianBlur(src,(13,13),cv2.BORDER_DEFAULT)

Now there is a clear distinction between the two images.

raja
Published on 02-May-2019 16:40:50
Advertisements