How to use Opencv to Blur an Image

Opencv is a open source computer vision library which is majorly used for its image processing feature. Whenever you come across some image filtering mobile apps, it’s highly likely that it was developed on top of Opencv library.

One of the frequently used image processing task everyone come across is blur or smoothing the image. Blur works on the principle of applying filters to the image. Filters are also called a kernels which will have some predefined values waited to be applied on the input pixel in order to get the blurred output pixel. In this post we will cover the common blur options available in the Opencv library.

There are 4 common blur option available in opencv.

  • Averaging
  • Gaussian Blurring
  • Median Blurring
  • Bilateral Filtering

Lets see a brief about each blurring algorithm.

Averaging or Normalized Box Filter

This is basic blurring option available in Opencv. It works on the principle of converting every input pixel into its kernel neighbour mean. kernel just a filter of some sized matrix to inform how much neighbours a pixel can relate to derive the output pixel. Lets say our kernel is 5 X 5 matrix with equal weight. In our case it covers 25 neighbours and the mean of them will be replaced with center pixel of the input image.

Function signature: blur(src, dst, ksize, anchor, borderType)

Gaussian Blur

It works based on the bell shaped curve of Gaussian distribution. So as like bell curve, the kernel image have more weight toward the closer neighbours and the weight decreases proportional to the distance between center pixel and the neighbour.

Function Signature: GaussianBlur(src, dst, ksize, sigmaX, sigmaY, borderType)

Median Blur

Input pixel will be replaced with median of its kernel neighbours. If the kernel is 3 X 3 matrix with equal weight.  The algorithm works to get median of its 9 neighbours and replace with the input pixel. One thing to note here is it won’t derive any new value for the output pixel instead it will replace with the respective median neighbour pixel. Hence this will help to remove any noise in the image at a greater level.

Function Signature: medianBlur(src, dst, ksize, borderType)

Bilateral Blur

Other algorithms only care about decreasing the noise level by smoothing the entire image. So they won’t care about whether the modifying pixel is on the edge or not. This leads to the blurred edges in the ouput which is bad per se.

Bilateral blur works similar to Gaussian blur with some additional computation to figure out whether the pixel is in edges or not. This additional computation is called gaussian function for intensity where it figures out whether the neighbour has similar intensity. Generally pixel around edges have high difference in intensity with the center using that information it will ignore those pixel for blurring.

Function Signature: bilateralFilter ( InputArray  src, OutputArray  dst, int  d, double  sigmaColor, double   sigmaSpace, int  borderType = BORDER_DEFAULT )

Below source code produce blurred imaged using different algorithm mentioned above using sliding as input. Remember to have the image in the right place before trying the code.

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv4/opencv2/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;


Mat src; Mat dst;
char window_name[] = "BLUR";
char slider_wnd_name[] ="BLUR ALGORITHM";
int slider;
int slider_max = 3;

int displayImage( int delay );

static void onTrackBar(int, void*) {
    switch(slider) {
        case 0:
            blur( src, dst, Size( 15 , 15 ), Point(-1,-1) );
            displayImage(0);
            break;
        case 1:
            GaussianBlur( src, dst, Size( 15, 15 ), 0, 0 );
            displayImage(0);
            break;
        case 2:
            medianBlur ( src, dst, 15 );
            displayImage(0);
            break;
        case 3:
            bilateralFilter ( src, dst, 15, 15*2, 15/2 );
            displayImage(0);
            break;
        default:
            cout<<" *** Invalid Input ***"<<endl;
            break;

    }


}
 int main( int argc, char** argv ) {
     namedWindow( window_name, WINDOW_AUTOSIZE );
     namedWindow( slider_wnd_name, WINDOW_AUTOSIZE  ); 
     char TrackbarName[50];
     sprintf( TrackbarName, "Blur Algorithm %d", slider_max );

     // Load the source image
     src = imread( "jalli.jpg", 1 );
     dst = src.clone();

     createTrackbar(TrackbarName, slider_wnd_name, &slider, 
      slider_max, onTrackBar);

     onTrackBar(slider, 0);
     waitKey(0);
     return 0;
 }


  int displayImage( int delay ) {
      imshow( window_name, dst );
      int c = waitKey ( delay );
      if( c >= 0 ) { return -1; }
      return 0;
  }

compile with following option >> g++ $(pkg-config –cflags –libs opencv4)  -std=c++11 blur.cpp  -o blur

Reference: Opencv documentation

Hope you would gotten some idea about image blurring using Opencv. Let me know your feedback in the comment section below.

Leave a Reply

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