高斯滤波器的原理及其实现过程
高斯滤波器是一种线性滤波器,能够有效的抑制噪声,平滑图像。其作用原理和均值滤波器类似,都是取滤波器窗口内的像素的均值作为输出。其窗口模板的系数和均值滤波器不同,均值滤波器的模板系数都是相同的为1;而高斯滤波器的模板系数,则随着距离模板中心的增大而系数减小。所以,高斯滤波器相比于均值滤波器对图像个模糊程度较小。
什么是高斯滤波器
既然名称为高斯滤波器,那么其和高斯分布(正态分布)是有一定的关系的。一个二维的高斯函数如下:
小数形式的模板,就是直接计算得到的值,没有经过任何的处理;
整数形式的,则需要进行归一化处理,将模板左上角的值归一化为1,下面会具体介绍。使用整数的模板时,需要在模板的前面加一个系数,系数为
也就是模板系数和的倒数。
void generateGaussianTemplate(double window[][11], int ksize, double sigma)
{
static const double pi = 3.1415926;
int center = ksize / 2; // 模板的中心位置,也就是坐标的原点
double x2, y2;
for (int i = 0; i < ksize; i )
{
x2 = pow(i - center, 2);
for (int j = 0; j < ksize; j )
{
y2 = pow(j - center, 2);
double g = exp(-(x2 y2) / (2 * sigma * sigma));
g /= 2 * pi * sigma;
window[i][j] = g;
}
}
double k = 1 / window[0][0]; // 将左上角的系数归一化为1
for (int i = 0; i < ksize; i )
{
for (int j = 0; j < ksize; j )
{
window[i][j] *= k;
}
}
}
void generateGaussianTemplate(double window[][11], int ksize, double sigma)
{
static const double pi = 3.1415926;
int center = ksize / 2; // 模板的中心位置,也就是坐标的原点
double x2, y2;
double sum = 0;
for (int i = 0; i < ksize; i )
{
x2 = pow(i - center, 2);
for (int j = 0; j < ksize; j )
{
y2 = pow(j - center, 2);
double g = exp(-(x2 y2) / (2 * sigma * sigma));
g /= 2 * pi * sigma;
sum = g;
window[i][j] = g;
}
}
//double k = 1 / window[0][0]; // 将左上角的系数归一化为1
for (int i = 0; i < ksize; i )
{
for (int j = 0; j < ksize; j )
{
window[i][j] /= sum;
}
}
}
void GaussianFilter(const Mat &src, Mat &dst, int ksize, double sigma)
{
CV_Assert(src.channels() || src.channels() == 3); // 只处理单通道或者三通道图像
const static double pi = 3.1415926;
// 根据窗口大小和sigma生成高斯滤波器模板
// 申请一个二维数组,存放生成的高斯模板矩阵
double **templateMatrix = new double*[ksize];
for (int i = 0; i < ksize; i )
templateMatrix[i] = new double[ksize];
int origin = ksize / 2; // 以模板的中心为原点
double x2, y2;
double sum = 0;
for (int i = 0; i < ksize; i )
{
x2 = pow(i - origin, 2);
for (int j = 0; j < ksize; j )
{
y2 = pow(j - origin, 2);
// 高斯函数前的常数可以不用计算,会在归一化的过程中给消去
double g = exp(-(x2 y2) / (2 * sigma * sigma));
sum = g;
templateMatrix[i][j] = g;
}
}
for (int i = 0; i < ksize; i )
{
for (int j = 0; j < ksize; j )
{
templateMatrix[i][j] /= sum;
cout << templateMatrix[i][j] << ' ';
}
cout << endl;
}
// 将模板应用到图像中
int border = ksize / 2;
copyMakeBorder(src, dst, border, border, border, border, BorderTypes::BORDER_REFLECT);
int channels = dst.channels();
int rows = dst.rows - border;
int cols = dst.cols - border;
for (int i = border; i < rows; i )
{
for (int j = border; j < cols; j )
{
double sum[3] = { 0 };
for (int a = -border; a <= border; a )
{
for (int b = -border; b <= border; b )
{
if (channels == 1)
{
sum[0] = templateMatrix[border a][border b] * dst.at<uchar>(i a, j b);
}
else if (channels == 3)
{
Vec3b rgb = dst.at<Vec3b>(i a, j b);
auto k = templateMatrix[border a][border b];
sum[0] = k * rgb[0];
sum[1] = k * rgb[1];
sum[2] = k * rgb[2];
}
}
}
for (int k = 0; k < channels; k )
{
if (sum[k] < 0)
sum[k] = 0;
else if (sum[k] > 255)
sum[k] = 255;
}
if (channels == 1)
dst.at<uchar>(i, j) = static_cast<uchar>(sum[0]);
else if (channels == 3)
{
Vec3b rgb = { static_cast<uchar>(sum[0]), static_cast<uchar>(sum[1]), static_cast<uchar>(sum[2]) };
dst.at<Vec3b>(i, j) = rgb;
}
}
}
// 释放模板数组
for (int i = 0; i < ksize; i )
delete[] templateMatrix[i];
delete[] templateMatrix;
}
// 分离的计算
void separateGaussianFilter(const Mat &src, Mat &dst, int ksize, double sigma)
{
CV_Assert(src.channels()==1 || src.channels() == 3); // 只处理单通道或者三通道图像
// 生成一维的高斯滤波模板
double *matrix = new double[ksize];
double sum = 0;
int origin = ksize / 2;
for (int i = 0; i < ksize; i )
{
// 高斯函数前的常数可以不用计算,会在归一化的过程中给消去
double g = exp(-(i - origin) * (i - origin) / (2 * sigma * sigma));
sum = g;
matrix[i] = g;
}
// 归一化
for (int i = 0; i < ksize; i )
matrix[i] /= sum;
// 将模板应用到图像中
int border = ksize / 2;
copyMakeBorder(src, dst, border, border, border, border, BorderTypes::BORDER_REFLECT);
int channels = dst.channels();
int rows = dst.rows - border;
int cols = dst.cols - border;
// 水平方向
for (int i = border; i < rows; i )
{
for (int j = border; j < cols; j )
{
double sum[3] = { 0 };
for (int k = -border; k <= border; k )
{
if (channels == 1)
{
sum[0] = matrix[border k] * dst.at<uchar>(i, j k); // 行不变,列变化;先做水平方向的卷积
}
else if (channels == 3)
{
Vec3b rgb = dst.at<Vec3b>(i, j k);
sum[0] = matrix[border k] * rgb[0];
sum[1] = matrix[border k] * rgb[1];
sum[2] = matrix[border k] * rgb[2];
}
}
for (int k = 0; k < channels; k )
{
if (sum[k] < 0)
sum[k] = 0;
else if (sum[k] > 255)
sum[k] = 255;
}
if (channels == 1)
dst.at<uchar>(i, j) = static_cast<uchar>(sum[0]);
else if (channels == 3)
{
Vec3b rgb = { static_cast<uchar>(sum[0]), static_cast<uchar>(sum[1]), static_cast<uchar>(sum[2]) };
dst.at<Vec3b>(i, j) = rgb;
}
}
}
// 竖直方向
for (int i = border; i < rows; i )
{
for (int j = border; j < cols; j )
{
double sum[3] = { 0 };
for (int k = -border; k <= border; k )
{
if (channels == 1)
{
sum[0] = matrix[border k] * dst.at<uchar>(i k, j); // 列不变,行变化;竖直方向的卷积
}
else if (channels == 3)
{
Vec3b rgb = dst.at<Vec3b>(i k, j);
sum[0] = matrix[border k] * rgb[0];
sum[1] = matrix[border k] * rgb[1];
sum[2] = matrix[border k] * rgb[2];
}
}
for (int k = 0; k < channels; k )
{
if (sum[k] < 0)
sum[k] = 0;
else if (sum[k] > 255)
sum[k] = 255;
}
if (channels == 1)
dst.at<uchar>(i, j) = static_cast<uchar>(sum[0]);
else if (channels == 3)
{
Vec3b rgb = { static_cast<uchar>(sum[0]), static_cast<uchar>(sum[1]), static_cast<uchar>(sum[2]) };
dst.at<Vec3b>(i, j) = rgb;
}
}
}
delete[] matrix;
}
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
double sigmaX, double sigmaY = 0,
int borderType = BORDER_DEFAULT );
赞 (0)