Wednesday 24 February 2016

Opencv C++ Code for Low Pass Averaging Filter

This Opencv Tutorial is about Low Pass Filter.
Low Pass Filter:
As the name suggests it only pass the low frequency components of an image and removes the high frequency content from it.
Thus it is used for image smoothing and reducing the disparity among the image pixels.
Generally the Low pass filter kernel (mask) size is preferred as odd e.g 3,5,7 etc since it helps to in maintaining the symmetry.
The larger the kernel size ,more smoother the image becomes.But also as the kernel size increases ,the image becomes more blur.
Low pass filter also reduces the edges in an Image.
Types of low pass filter:
1. Low pass Averaging Filter
2. Low pass Median Filter

In this Example we have explained Low Pass Averaging Filter.
Low pass Averaging Filter:
This filter works best when there is a Gaussian noise added to the image.
As it can be seen from the low pass filter mask,it is basically the average of all the neighbouring pixels along with it.
Characteristic of Low Pass Kernel:

  • All the coefficients of the mask are positive
  • Sum of all the mask Elements should equal to 1
Working:
Consider an 6*6 image matrix:

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

As it can be seen from the image matrix there is sharp edge 10 and 20.
And the low-pass filter 3*3 mask as:
1/9 1/9 1/9
1/9 1/9 1/9
1/9 1/9 1/9

Note:
The above array is an example of one possible kernel for a low pass filter. Other filters may include more weighting for the center point, or have different smoothing in each dimension.
Now we multiply each element of the mask with that of the image pixels,add the result and obtain its average.And replace the resultant value with that of the centre pixels.We cannot work with the borders,hence they are left as it is.
This procedure is followed continuously by shifting the mask over the image starting from the top-left corner to that of the bottom-right corner.
20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

20 20 20 20 20
20 20 20 20 20
20 20 20 20 20
10 10 10 10 10
10 10 10 10 10

The resultant matrix thus obtained is:
20 20 20 20 20
20 20 20 20 20
20 16.66 16.66 16.66 20
10 13.33 13.33 13.33 10
10 10 10 10 10

Thus ,it can be seen that the sharpness of the edge is reduced to a considerable extent and the edge between 10 and 20 is blurred
Here is the OpenCV Code for implementing low pass filter over an Image:

//Opencv C++ Tutorial on Low Pass Averaging Filter
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "iostream"
 
using namespace cv;
using namespace std;
 
int main( )
{
    Mat src1,src2;
 int a;
 Scalar intensity1=0;
    src1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.png", CV_LOAD_IMAGE_GRAYSCALE);
 src2 = src1.clone();

 //If image not found 
 if (!src1.data)                                                                          
     {  
      cout << "No image data \n";  
      return -1;  
     } 

 //Take the mask size
 //It should be less than that of size of the image
  cout<<"Enter the mask Size =";
  cin>>a;

 //for loop for counting the number of rows and columns and displaying the pixel value at each point
   for (int i = 0; i < src1.rows-a; i++) 
   { 
    for (int j = 0; j < src1.cols-a; j++) 
     { 
   Scalar intensity2;
   for (int p = 0; p<a; p++) 
       { 
    
       for (int q = 0; q<a; q++) 
        { 

      intensity1 = src1.at<uchar>(i+p,j+q); 
      intensity2.val[0] +=intensity1.val[0];
     }
  }
    src2.at<uchar>(i+(a-1)/2,j+(a-1)/2)=intensity2.val[0]/(a*a);
   } 
    }

   //Display the original image
    namedWindow("Display Image");                
    imshow("Display Image", src1);

   //Display the Low Pass Filtered Image image
  namedWindow("Low Pass Filtered Image");     
     imshow("Low Pass Filtered Image", src2);  
     waitKey(0);
     return 0;
        }

Input Image:
Opencv C++ Code for Low Pass Averaging Filter

Output Image:
Opencv C++ Code for Low Pass Averaging Filter Output

Note: Here we have Choosen Mask Size=5.

No comments:

Post a Comment