FFT和IFFT - Matlab和openCV中结果之间的差异 [英] FFT and IFFT - difference between results in Matlab and openCV

查看:603
本文介绍了FFT和IFFT - Matlab和openCV中结果之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用openCV将这个简单的Matlab代码转换为C ++:

I'm trying to convert this simple Matlab code to C++ with openCV:

localstd=sqrt(abs(ifft2(fft2(output).*gf)));

这意味着取矩阵输出的fft,将元素与矩阵相乘 gf,然后取出那个,然后取其大小。

It means taking the fft of the matrix "output", multiplying it element by element with the matrix "gf", then taking the ifft of that and then taking the magnitude of that.

我正在尝试以下简单代码:

I'm trying the following simple code:

Mat complexI;
dft(output, complexI,cv::DFT_SCALE||DFT_COMPLEX_OUTPUT);
Mat copmlexI2=Mat(n,n,CV_32F);
mulSpectrums(complexI,gf,copmlexI2,DFT_COMPLEX_OUTPUT);
dft(copmlexI2,copmlexI2,cv::DFT_INVERSE||DFT_COMPLEX_OUTPUT);

Mat planes[]= {Mat::zeros(output.size(), CV_32F), Mat::zeros(output.size(), CV_32F)};;
split(copmlexI2, planes);                   // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude

Mat localstd = planes[0];

for (int i=0;i<localstd.rows;i++){
    for (int j=0;j<localstd.cols;j++){
        localstd.at<float>(i,j)= sqrt(localstd.at<float>(i,j));
    }
}

for (int i=0;i<localstd.rows;i++){
        for (int j=0;j<localstd.cols;j++){
            localstd.at<float>(i,j)/= 255;
        }
    }

这很简单。我正在取输出的dft,将它的频谱乘以df然后取出ifft。接下来,我将结果分成实际和想象的平面最后,我拿出平方米并用255除以标准化。

It's very simple. I'm taking the dft of "output", multiply it's spectrum with "df" and take the ifft of that. Next, I split the result into the real and imaginary plane and take the magnitude. Finally, I take the sqrt of that and normalize by dividing with 255.

我得到的结果与我在Matlab中得到的结果非常不同。我在这里错过了吗?关于如何修复代码的任何想法?

The results I get are very different than what I get in Matlab. What am I missing here? Any ideas on how to fix the code?

谢谢提前!

推荐答案

这是不正确的

cv::DFT_INVERSE||DFT_COMPLEX_OUTPUT

,如果你想要组合二进制值你应该使用二进制或:

, if you want combine binary values you should use "binary or":

cv::DFT_INVERSE | DFT_COMPLEX_OUTPUT

或+ operation

or + operation

cv::DFT_INVERSE + DFT_COMPLEX_OUTPUT

A || B - 符合逻辑或。 A,B和结果可能只是真或假。

A || B - is logical or. A, B and result may be only true or false.

A | B - 是按位或。

A | B - is bitwise or.

你也可以尝试DFT_SCALE标志。

You can also try DFT_SCALE flag.


DFT_SCALE缩放结果:除以数组
元素的数量。通常情况下,它与DFT_INVERSE结合使用。

DFT_SCALE scales the result: divide it by the number of array elements. Normally, it is combined with DFT_INVERSE.

这篇关于FFT和IFFT - Matlab和openCV中结果之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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