Python OpenCV – Image Smoothing using Averaging, Gaussian Blur and Median Filter


Images may contain various types of noises that reduce the quality of the image. Blurring or smoothing is the technique for reducing the image noises and improve its quality. Usually, it is achieved by convolving an image with a low pass filter that removes high-frequency content like edges from the image. In this tutorial, we will see methods of Averaging, Gaussian Blur, and Median Filter used for image smoothing and how to implement them using python OpenCV,  built-in functions of cv2.blur(), cv2.GaussianBlur(), cv2.medianBlur().

Let us first import the OpenCV library.

Importing OpenCV Library

In [1]:
import cv2

Image used for this Tutorial

Noise Image
Image with Noise

Averaging: cv2.blur()

Averaging of the image is done by applying a convolution operation on the image with a normalized box filter. In convolution operation, the filter or kernel is slides across an image and the average of all the pixels is found under the kernel area and replace this average with the central element of the image.

Note: The smoothing of an image depends upon the kernel size. If Kernel size is large then it removes the small feature of the image. But if the kernel size is too small then it is not able to remove the noise.


cv2.blur(src, ksize, dst, anchor, borderType)

src: It is the image whose is to be blurred.

ksize: A tuple representing the blurring kernel size.

dst: It is the output image of the same size and type as src.

anchor: It is a variable of type integer representing anchor point and it’s default value Point is (-1, -1) which means that the anchor is at the kernel center.

borderType: It depicts what kind of border to be added. It is defined by flags like cv2.BORDER_CONSTANT, cv2.BORDER_REFLECT, etc

Return Value: It returns an image.

Example of Smoothing Image using cv2.blur()

In [2]:
#reading an image
In [3]:
#We are taking Kernel Size as 5x5
img_blur = cv2.blur(img_first,(5,5))
In [4]:
#Display blur image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
Result of Image Averaging with cv2.blur()
Result of Image Averaging with cv2.blur()

[adrotate banner=”3″]

Gaussian Blur: cv2.GaussianBlur()

In the gaussian blur technique, the image is convolved with a gaussian filter instead of a box or normalized filter. Gaussian blur OpenCV function has the following syntax.


cv2.GaussianBlur( src, dst, size, sigmaX, sigmaY = 0, borderType =BORDER_DEFAULT)

src It is the image whose is to be blurred.

dst output image of the same size and type as src.

ksize Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd.

sigmaX Gaussian kernel standard deviation in X direction.

sigmaY Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be equal to
sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height, respectively

borderType: Specifies image boundaries while kernel is applied on image borders.

Example of Smoothing Image using cv2.GaussianBlur()

In [5]:
#Read Image

In [6]:

#We are taking Kernel size as 5x5
gaussian_blur = cv2.GaussianBlur(img_second,(5,5),sigmaX=0)
In [7]:
#Display gaussian blur image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
Result of Gaussian Blur with cv2.GaussianBlur()
Result of Gaussian Blur with cv2.GaussianBlur()

Median Filter: cv2.medianBlur()

The median filter technique is very similar to the averaging filtering technique shown above. The only difference is cv2.medianBlur() computes the median of all the pixels under the kernel window and the central pixel is replaced with this median value instead of the average value.

Note: This is highly effective in removing salt-and-pepper noise.


cv2.medianBlur( src,dst,ksize )

src : It is the image that is to be blurred.

dst : destination array of the same size and type as src.

ksize : aperture linear size; it must be odd and greater than 1, for example 3, 5, 7 …

Example of Smoothing Image using cv2.medianBlur()

In [8]:
In [9]:
image_median = cv2.medianBlur(img_third,5)

Median Blurring always reduces the noise effectively because in this filtering technique the central element is always replaced by some pixel value in the image. But in the above filters, the central element is a newly calculated value which may be a pixel value in the image or a new value.

In [10]:
#Display MedianBlurred image
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
Result of Median Filter with cv2.medianBlur()
Result of Median Filter with cv2.medianBlur()

As you can see here the salt pepper noise gets drastically reduced using cv2.medianBlur() OpenCV function


Reaching the end of this tutorial, we learned image smoothing techniques of Averaging, Gaussian Blur, and Median Filter and their python OpenCV implementation using cv2.blur() , cv2.GaussianBlur() and cv2.medianBlur().

Reference –


  • Sachin Mohan

    My name is Sachin Mohan, an undergraduate student of Computer Science and Engineering. My area of interest is ‘Artificial intelligence’ specifically Deep learning and Machine learning. I have attended various online and offline courses on Machine learning and Deep Learning from different national and international institutes My interest toward Machine Learning and deep Learning made me intern at ISRO and also I become the 1st Runner up in TCS EngiNX 2019 contest. I always love to share my knowledge and experience and my philosophy toward learning is "Learning by doing". So thats why I believe in education which have include both theoretical as well as practical knowledge.

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *