Low pass filters (blurring) in Image Processing using C++

Theory

Low pass filtering also called “blurring” & “smoothing” is very basic filtering operations in image processing. The simplest low-pass filter just calculates the average of a pixel and all of its eight immediate neighbors. The result replaces the original value of the pixel. The process is repeated for every pixel in the image. The example of Kernel used for simple low pass filters is

In this kernel pixel values from the neighborhood are summed without being weighted, and the sum is divided by the number of pixels in the neighborhood. Here is the C++ code for low pass filtering operation using above Kernel (Averaging operation). Here I implement the convolution operation.

Source Code : C++ 

#include
#include
#include
 
using namespace std;
using namespace cv;
 
int main()
{
 
      Mat src, dst;
      float sum;
 
      /// Load an image
      src = imread("salt.jpg", CV_LOAD_IMAGE_GRAYSCALE);
 
      if( !src.data )
      { return -1; }
 
      // define the kernel
      float Kernel[3][3] = {
                            {1/9.0, 1/9.0, 1/9.0},
                            {1/9.0, 1/9.0, 1/9.0},
                            {1/9.0, 1/9.0, 1/9.0}
                           }; 
         dst = src.clone();
        for(int y = 0; y (y,x) = 0.0;
        //convolution operation
        for(int y = 1; y (y - j, x - k);
                    }
                }
                dst.at(y,x) = sum;
            }
        }
 
        namedWindow("final");
        imshow("final", dst);
 
        namedWindow("initial");
        imshow("initial", src);
 
      waitKey();
 
 
    return 0;
}

 

Output

Loading…

 

Loading…