C ++中的高斯滤波器生成
众所周知,高斯滤波在图像处理领域非常有用。用于减少图像的噪点。在本节中,我们将看到如何生成2D高斯内核。用于生成2D内核的高斯分布如下。
$$G(x,y)=\frac{1}{2\Pi\:\sigma^{2}}e^{\frac{x^{2}+y^{2}}{2\sigma^{2}}}$$
示例
让我们看下面的实现以更好地理解-
#include <cmath>
#include <iomanip>
#include <iostream>
#define PI 3.1415
using namespace std;
void calc_filter(double kernel[][5]) {
double sigma = 1.0;
double p, q = 2.0 * sigma * sigma;
double sum = 0.0;
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
p = sqrt(x * x + y * y);
kernel[x + 2][y + 2] = (exp(-(p * p) / q)) / (PI * q);
sum += kernel[x + 2][y + 2];
}
}
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
kernel[i][j] /= sum;
}
int main() {
double kernel[5][5];
calc_filter(kernel);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j)
cout << kernel[i][j] << " ";
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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短