OpenCV中的逆傅里叶变换 [英] Inverse fourier transformation in OpenCV

查看:432
本文介绍了OpenCV中的逆傅里叶变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenCV和图像处理算法的新手。我需要在C ++中的OpenCV中做离散傅里叶逆变换,但我不知道如何。我在互联网上搜索,我没有找到答案。我在我的程序中使用此页面中的代码进行转换: http: //opencv.itseez.com/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html 。我试图做反向的代码,但我不知道我做错了。我的代码在这里(我认为整个代码是错误的):

I am new in OpenCV and image processing algorithms. I need to do inverse discrete fourier transformation in OpenCV in C++, but I don't know how. I searched over internet and I didn't find answer. I am doing fourier transformation in my program with this code from this page: http://opencv.itseez.com/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html. I have tried to do inverse to that code, but I don't know where I am doing wrong. My code is here (I think that whole code is wrong):

void doFourierInverse(const Mat &src, Mat &dst) {
  normalize(src, dst, 0, -1, CV_MINMAX); 

  int cx = dst.cols/2;
  int cy = dst.rows/2;

  Mat q0(dst, Rect(0, 0, cx, cy));   
  Mat q1(dst, Rect(cx, 0, cx, cy));  
  Mat q2(dst, Rect(0, cy, cx, cy));  
  Mat q3(dst, Rect(cx, cy, cx, cy)); 

  Mat tmp;         
  q0.copyTo(tmp);
  q3.copyTo(q0);
  tmp.copyTo(q3);

  q1.copyTo(tmp);      
  q2.copyTo(q1);
  tmp.copyTo(q2);

  dst = dst(Rect(0, 0, dst.cols & -2, dst.rows & -2));

  exp(dst, dst);
  dst -= Scalar::all(1);  

  Mat planes[2];

  polarToCart(dst, Mat::zeros(dst.rows, dst.cols, dst.type()), planes[0], planes[1]);

  merge(planes, 2, dst);    

  idft(dst, dst, DFT_INVERSE | DFT_SCALE); 

  split(dst, planes);   

  dst = planes[0];
}


推荐答案

必须交换不同的象限,它只需要你是一个人类,需要一个更自然的可视化的FFT结果(即0频率在中间,负频率左/下和正频率上/右)。

Actually, you don't have to swap the different quadrants, it's needed only if you're a human and want a more natural looking visualization of the FFT result (i.e. with the 0 frequency in the middle, negative frequencies left/bottom and positive frequencies up/right).

要反转FFT,您需要将正向变换的结果按原样传递(或在您想要的频率过滤之后)到同一个dft ,只添加标志DFT_INVERSE。如果你记得你对FFT的数学,前向和后向变换在公式中有非常紧的纽结...

To invert the FFT, you need to pass the result of the forward transform "as is" (or after the frequency filtering you wanted) to the same dft() function, only adding the flag DFT_INVERSE. If you remember your math about FFT, the forward and backward transforms have very tight kinks in the formulation...

--- EDIT ---

--- EDIT ---

究竟什么不工作?
以下代码执行向前 - 向后FFT,并且一切都按预期正常工作。

What exactly doesn't work ? The following code does perform forward-then-backward FFT, and everything works just fine as expected.

// Load an image
cv::Mat inputImage = cv::imread(argv[argc-1], 0);

// Go float
cv::Mat fImage;
inputImage.convertTo(fImage, CV_32F);

// FFT
std::cout << "Direct transform...\n";
cv::Mat fourierTransform;
cv::dft(fImage, fourierTransform, cv::DFT_SCALE|cv::DFT_COMPLEX_OUTPUT);

// Some processing
doSomethingWithTheSpectrum();

// IFFT
std::cout << "Inverse transform...\n";
cv::Mat inverseTransform;
cv::dft(fourierTransform, inverseTransform, cv::DFT_INVERSE|cv::DFT_REAL_OUTPUT);

// Back to 8-bits
cv::Mat finalImage;
inverseTransform.convertTo(finalImage, CV_8U);

这篇关于OpenCV中的逆傅里叶变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆