1. 高斯滤波原理

Xa = exp(-1*(0-1)(0-1)/(2*1*1))= 0.606530659712633
X = exp(-1*(1-1)(1-1)/(2*1*1))= 1
Xb = exp(-1*(2-1)(2-1)/(2*1*1))= 0.606530659712633


Sum = Xa + X + Xb = 2.2130613194252668
Xa = Xa/Sum = 0.274068619061197
X = X/Sum = 0.451862761877606
Xb = Xb/Sum = 0.274068619061197


Mat kernelX = getGaussianKernel(3, 1);
cout << kernelX <<  endl;


2. 图像二维卷积

Mat kernelX = getGaussianKernel(3, 1);
cout << kernelX <<  endl;

Mat kernelY = getGaussianKernel(3, 1);
Mat G = kernelX * kernelY.t();
cout << G << endl << endl << endl;


3. 具体实现

#include <iostream>
#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
//从文件中读取成灰度图像
const char* imagename = "C:\\Data\\imgDemo\\lena.jpg";
if (img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}

//直接高斯滤波
Mat dst1;
GaussianBlur(img, dst1, Size(3, 3), 1, 1);

//自定义高斯滤波器
Mat kernelX = getGaussianKernel(3, 1);
Mat kernelY = getGaussianKernel(3, 1);
Mat G = kernelX * kernelY.t();
Mat dst2;
filter2D(img, dst2, -1, G);

//比较两者的结果
Mat c;
compare(dst1, dst2, c, CMP_EQ);

//
imshow("原始", img);
imshow("高斯滤波1", dst1);
imshow("高斯滤波2", dst2);
imshow("比较结果", c);

waitKey();

return 0;
}


4. 参考资料

posted @ 2019-03-25 11:06  charlee44  阅读(13408)  评论(0编辑  收藏  举报