什么是高/低通滤波,使用C++实现一个图像的高通滤波

低通滤波与高通滤波相对。本文主要介绍高通滤波。

高通滤波器的工作原理是,它首先会将图像转换到频率域,即使用傅里叶变换将图像从空间域转换到频率域。在频率域中,低频信号对应于图像的平滑区域,而高频信号对应于图像的边缘和细节。

高通滤波器会将低频信号抑制或消除,而让高频信号通过。这样,当滤波器再次将图像从频率域转换回空间域时,平滑区域会被减弱或消除,而边缘和细节会被增强。

这种高通滤波后的图像通常会看起来更加锐利和清晰,因为边缘和细节被突出了,而平滑区域被减少了。这在许多应用中都是有用的,比如边缘检测、图像分割、纹理分析等。

此外,高通滤波在信号处理、电子通信等领域中也有广泛应用。在这些领域中,高通滤波器可以用来消除低频干扰,提高信号的质量或传递效率。

ubuntu上安装fftw3库:
sudo apt-get install libfftw3-dev

#include <fftw3.h>
#include <complex>
#include <vector>
#include <iostream>

using namespace std;

// 执行FFT变换
vector<complex<double>> fft(const vector<double>& data) {
    int N = data.size();
    fftw_complex* out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    fftw_plan p = fftw_plan_dft_r2c_1d(N, &data[0], out, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    vector<complex<double>> result(N / 2 + 1);
    for (int i = 0; i < N / 2 + 1; ++i) {
        result[i] = complex<double>(out[i][0], out[i][1]);
    }
    fftw_free(out);
    return result;
}

// 执行IFFT变换
vector<double> ifft(const vector<complex<double>>& data) {
    int N = data.size() * 2 - 2;
    fftw_complex* in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    fftw_complex* out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    for (int i = 0; i < N / 2 + 1; ++i) {
        in[i][0] = data[i].real();
        in[i][1] = data[i].imag();
    }
    fftw_plan p = fftw_plan_dft_c2r_1d(N, in, out, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    vector<double> result(N / 2 + 1);
    for (int i = 0; i < N / 2 + 1; ++i) {
        result[i] = out[i][0] / N;
    }
    fftw_free(in);
    fftw_free(out);
    return result;
}

// 高通滤波器
vector<double> high_pass_filter(const vector<double>& signal, double cutoff_freq) {
    int N = signal.size();
    vector<complex<double>> fft_result = fft(signal);
    // 应用高通滤波器
    for (int i = 0; i < fft_result.size(); ++i) {
        if (i < cutoff_freq) {
            fft_result[i] = 0; // 移除低频分量
        }
    }
    // 执行IFFT变换得到滤波后的信号
    return ifft(fft_result);
}

int main() {
    // 示例信号
    vector<double> signal = {1, 2, 3, 4, 5, 4, 3, 2, 1};
    // 执行高通滤波
    double cutoff_freq = 2; // 截止频率
    vector<double> filtered_signal = high_pass_filter(signal, cutoff_freq);
    // 输出滤波后的信号
    for (double s : filtered_signal) {
        cout << s << " ";
    }
    cout << endl;
    return 0;
}

编译:
g++ -o high_pass_filter high_pass_filter.cpp -lfftw3

请注意,这个例子中的截止频率cutoff_freq是一个整数,并且应该基于你的信号长度和所需的滤波效果来选择。

在实际应用中,你可能***需要对输入信号进行填零(padding)以改善FFT的性能和准确性,尤其是在信号长度不是2的幂时。此外,对于不同的应用,可能需要不同的窗函数来减少频谱泄漏***。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++编写代码实现空域平滑、空域锐化、滤波高通滤波、巴特沃斯滤波和巴特沃斯高通滤波的功能的例子: 1. 空域平滑(均值滤波) ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); Mat smoothed; blur(image, smoothed, Size(3, 3)); imshow("Smoothed Image", smoothed); waitKey(0); return 0; } ``` 2. 空域锐化(拉普拉斯滤波) ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); Mat sharpened; Mat blurred; GaussianBlur(image, blurred, Size(3, 3), 0); addWeighted(image, 1.5, blurred, -0.5, 0, sharpened); imshow("Sharpened Image", sharpened); waitKey(0); return 0; } ``` 3. 滤波 ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); Mat filtered; GaussianBlur(image, filtered, Size(3, 3), 0); imshow("Filtered Image", filtered); waitKey(0); return 0; } ``` 4. 高通滤波 ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); Mat filtered; Mat blurred; GaussianBlur(image, blurred, Size(3, 3), 0); subtract(image, blurred, filtered); imshow("Filtered Image", filtered); waitKey(0); return 0; } ``` 5. 巴特沃斯滤波 ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); Mat filtered; Mat padded; int dft_M = getOptimalDFTSize(image.rows); int dft_N = getOptimalDFTSize(image.cols); copyMakeBorder(image, padded, 0, dft_M - image.rows, 0, dft_N - image.cols, BORDER_CONSTANT, Scalar::all(0)); Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)}; Mat complexI; merge(planes, 2, complexI); dft(complexI, complexI); float cutoff_frequency = 30.0; float order = 2.0; float d0 = cutoff_frequency * dft_M / 100.0; for (int i = 0; i < complexI.rows; i++) { for (int j = 0; j < complexI.cols; j++) { float d = sqrt(pow(i - complexI.rows / 2, 2) + pow(j - complexI.cols / 2, 2)); float h = 1.0 / (1.0 + pow(d / d0, 2 * order)); complexI.at<Vec2f>(i, j)[0] *= h; complexI.at<Vec2f>(i, j)[1] *= h; } } idft(complexI, filtered, DFT_SCALE | DFT_REAL_OUTPUT); filtered = filtered(Rect(0, 0, image.cols, image.rows)); imshow("Filtered Image", filtered); waitKey(0); return 0; } ``` 6. 巴特沃斯高通滤波 ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("input.jpg"); Mat filtered; Mat padded; int dft_M = getOptimalDFTSize(image.rows); int dft_N = getOptimalDFTSize(image.cols); copyMakeBorder(image, padded, 0, dft_M - image.rows, 0, dft_N - image.cols, BORDER_CONSTANT, Scalar::all(0)); Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)}; Mat complexI; merge(planes, 2, complexI); dft(complexI, complexI); float cutoff_frequency = 30.0; float order = 2.0; float d0 = cutoff_frequency * dft_M / 100.0; for (int i = 0; i < complexI.rows; i++) { for (int j = 0; j < complexI.cols; j++) { float d = sqrt(pow(i - complexI.rows / 2, 2) + pow(j - complexI.cols / 2, 2)); float h = 1.0 / (1.0 + pow(d0 / d, 2 * order)); complexI.at<Vec2f>(i, j)[0] *= h; complexI.at<Vec2f>(i, j)[1] *= h; } } idft(complexI, filtered, DFT_SCALE | DFT_REAL_OUTPUT); filtered = filtered(Rect(0, 0, image.cols, image.rows)); imshow("Filtered Image", filtered); waitKey(0); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值