You are on page 1of 2

Gaussian filtering is extensively used in Image Processing to reduce the noise of an image.

In this article I will generate the 2D Gaussian Kernel that follows the Gaussian Distribution which is given

Where is the standard deviation of distribution, x is the distance from the origin in the horizontal axis, y is the distance from the origin in the vertical axis. The mean is assumed to be at origin O(0,0). The pictorial view of Gaussian Distribution for = 0 and mean at origin is

Source Code in C/C++ ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

#include <iostream> #include <cmath> #include <iomanip> using namespace std; void createFilter(double gKernel[][5]) { // set standard deviation to 1.0 double sigma = 1.0; double r, s = 2.0 * sigma * sigma; // sum is for normalization double sum = 0.0; // generate 5x5 kernel for (int x = -2; x <= 2; x++) {

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

for(int y = -2; y <= 2; y++) { r = sqrt(x*x + y*y); gKernel[x + 2][y + 2] = (exp(-(r*r)/s))/(M_PI * s); sum += gKernel[x + 2][y + 2]; } } // normalize the Kernel for(int i = 0; i < 5; ++i) for(int j = 0; j < 5; ++j) gKernel[i][j] /= sum; } int main() { double gKernel[5][5]; createFilter(gKernel); for(int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) cout<<gKernel[i][j]<<"\t"; cout<<endl; } }

Output 5x5 Gaussian Kernel

You might also like