📜  C++中的高斯滤波器生成

📅  最后修改于: 2021-05-04 13:13:16             🧑  作者: Mango

高斯滤波被广泛应用于图像处理领域。它用于减少图像的噪点。在本文中,我们将生成2D高斯核。 2D高斯核遵循以下给定的高斯分布。
G(x, y)=\frac{1}{2\pi \sigma ^{2}}e^{-\frac{x^{2}+y^{2}}{2\sigma ^{2}}}
其中,y是沿垂直轴距原点的距离,x是沿水平轴距原点的距离,σ是标准偏差。

用C++实现

// C++ prgroam to generate Gaussian filter
#include 
#include 
#include 
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;
}
  
// Driver program to test above function
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;
    }
}

输出:

0.00296902    0.0133062    0.0219382    0.0133062    0.00296902    
0.0133062    0.0596343    0.0983203    0.0596343    0.0133062    
0.0219382    0.0983203    0.162103    0.0983203    0.0219382    
0.0133062    0.0596343    0.0983203    0.0596343    0.0133062    
0.00296902    0.0133062    0.0219382    0.0133062    0.00296902

参考:
https://zh.wikipedia.org/wiki/高斯过滤器