我想在确定正确的填充大小后创建一个高斯高通滤波器(例如,如果图像宽度和高度为 10X10,则应为 20X20)。
我有 Matlab 代码,我正尝试将其移植到 OpenCV 中,但我很难正确移植它。我的 Matlab 代码如下所示:
f1_seg = imread('thumb1-small-test.jpg');
Iori = f1_seg;
% Iori = imresize(Iori, 0.2);
%Convert to grayscale
I = Iori;
if length(size(I)) == 3
I = rgb2gray(Iori);
end
%
%Determine good padding for Fourier transform
PQ = paddedsize(size(I));
I = double(I);
%Create a Gaussian Highpass filter 5% the width of the Fourier transform
D0 = 0.05*PQ(1);
H = hpfilter('gaussian', PQ(1), PQ(2), D0);
% Calculate the discrete Fourier transform of the image.
F=fft2(double(I),size(H,1),size(H,2));
% Apply the highpass filter to the Fourier spectrum of the image
HPFS_I = H.*F;
我知道如何在 OpenCV 中使用 DFT,并且能够生成它的图像,但我不确定如何创建高斯滤波器。请指导我如何创建如上所示的高通高斯滤波器?
最佳答案
我相信您遇到的问题是 OpenCV 提供的高斯滤波器是在空间(时间)域中创建的,但您想要在频域中创建滤波器。 Here是一篇关于频域中高通滤波和低通滤波之间差异的好文章。
一旦您很好地理解了频域滤波的工作原理,您就可以尝试在频域中创建高斯滤波器了。 Here是关于在频域中创建一些不同(包括高斯)滤波器的好讲座。
如果您仍然遇到困难,稍后我会尝试用示例更新我的帖子!
编辑: 这是我写的关于实现高斯高通滤波器的一个简短示例(基于我向您介绍的讲座):
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
double pixelDistance(double u, double v)
{
return cv::sqrt(u*u + v*v);
}
double gaussianCoeff(double u, double v, double d0)
{
double d = pixelDistance(u, v);
return 1.0 - cv::exp((-d*d) / (2*d0*d0));
}
cv::Mat createGaussianHighPassFilter(cv::Size size, double cutoffInPixels)
{
Mat ghpf(size, CV_64F);
cv::Point center(size.width / 2, size.height / 2);
for(int u = 0; u < ghpf.rows; u++)
{
for(int v = 0; v < ghpf.cols; v++)
{
ghpf.at<double>(u, v) = gaussianCoeff(u - center.y, v - center.x, cutoffInPixels);
}
}
return ghpf;
}
int main(int /*argc*/, char** /*argv*/)
{
Mat ghpf = createGaussianHighPassFilter(Size(128, 128), 16.0);
imshow("ghpf", ghpf);
waitKey();
return 0;
}
无论如何,这绝对不是一个优化的过滤器生成器,但我试图让它简单明了以便于理解 :) 不管怎样,这段代码显示了以下过滤器:
注意:此滤波器未进行 FFT 移位(即,此滤波器在 DC 位于中心而不是左上角时起作用)。查看 OpenCV dft.cpp关于如何在 OpenCV 中执行 FFT 移位的示例(特别是第 62 - 74 行)。
尽情享受吧!
关于image - 如何将高斯滤波器应用于 OpenCV 中的 DFT 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578930/