1. 高斯低通滤波器
高斯低通滤波器(GLPF)是一种具有平滑频域特性、较慢衰减速度和良好截止频率附近衰减效果的滤波器。在图像处理中有着广泛的应用。
高斯低通滤波器的传播函数有如下的形式:
其中,D(u,v) 表示中心点到频域中心的距离,即 ,
是关于中心分离度的测度。令 ,则:
其中, 是截止频率,控制着滤波器的截止范围。
当 时,高斯低通滤波器下降到它最大值的 0.607 处。
值越大,允许通过的频率越高,滤波效果越弱; 值越小,允许通过的频率越低,滤波效果越强。
高斯低通滤波器具有以下特性:
平滑的频率响应: GLPF 的频率响应曲线呈高斯形状,在截止频率附近平滑衰减,在截止频率以上迅速衰减至零。这种平滑的频率响应使得 GLPF 能够有效地去除高频噪声而又不失真低频信号。
良好的边缘保持能力: 由于 GLPF 的频率响应在截止频率附近比较平滑,因此它不会对图像的边缘造成明显的振铃效应,从而能够较好地保持图像的边缘细节。
可控的截止频率: GLPF 的截止频率可以通过调整高斯函数的方差来控制,从而可以根据不同的应用需求来选择合适的截止频率。
下面的例子,展示了高斯低通滤波器的实现代码
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <random>
using namespace std;
using namespace cv;
void addSaltNoise(Mat &src, int num, Mat &dst)
{
dst = src.clone();
// 随机数产生器
std::random_device rd; //种子
std::mt19937 gen(rd()); // 随机数引擎
auto rows = src.rows; // 行数
auto cols = src.cols * src.channels();
for (int i = 0; i < num; i++)
{
auto row = static_cast<int>(gen() % rows);
auto col = static_cast<int>(gen() % cols);
auto p = dst.ptr<uchar>(row);
p[col++] = 255;
p[col++] = 255;
p[col] = 255;
}
}
// 高斯低通滤波核函数
cv::Mat gaussian_low_pass_kernel(cv::Mat scr, float sigma)
{
cv::Mat gaussianBlur(scr.size(), CV_32FC1);
float d0 = sigma;
for (int i = 0; i < scr.rows; i++) {
for (int j = 0; j < scr.cols; j++) {
float d = pow(float(i - scr.rows / 2), 2) + pow(float(j - scr.cols / 2), 2);//分子,计算pow必须为float型
gaussianBlur.at<float>(i, j) = expf(-d / (2 * d0*d0));
}
}
return gaussianBlur;
}
// fft 变换后进行频谱中心化
void fftshift(cv::Mat &plane0, cv::Mat &plane1)
{
int cx = plane0.cols / 2;
int cy = plane0.rows / 2;
cv::Mat q0_r(plane0, cv::Rect(0, 0, cx, cy)); // 元素坐标表示为(cx, cy)
cv::Mat q1_r(p