forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateGaussianFilter.cpp
More file actions
43 lines (36 loc) · 853 Bytes
/
createGaussianFilter.cpp
File metadata and controls
43 lines (36 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
// Generate Gaussian filter
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
// Function to create Gaussian filter
void FilterCreation(double GKernel[][5])
{
// intialising standard deviation to 1.0
double sigma = 1.0;
double r, s = 2.0 * sigma * sigma;
// sum is for normalization
double sum = 0.0;
// generating 5x5 kernel
for (int x = -2; x <= 2; x++) {
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];
}
}
// normalising 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];
FilterCreation(GKernel);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j)
cout << GKernel[i][j] << "\t";
cout << endl;
}
}