简体   繁体   English

FFT和IFFT-Matlab和openCV中结果之间的差异

[英]FFT and IFFT - difference between results in Matlab and openCV

I'm trying to convert this simple Matlab code to C++ with openCV: 我正在尝试使用openCV将这个简单的Matlab代码转换为C ++:

localstd=sqrt(abs(ifft2(fft2(output).*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. 这意味着取矩阵“输出”的ftf,将其逐元素乘以矩阵“ gf”,然后取其ifft,然后取其大小。

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;
        }
    }

It's very simple. 非常简单 I'm taking the dft of "output", multiply it's spectrum with "df" and take the ifft of that. 我用的是“输出”的dft,将其频谱乘以“ df”,然后用它的ifft。 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. 最后,我将其平方根除以255进行归一化。

The results I get are very different than what I get in Matlab. 我得到的结果与在Matlab中得到的结果有很大不同。 What am I missing here? 我在这里想念什么? Any ideas on how to fix the code? 关于如何修复代码的任何想法?

Thanks in advance! 提前致谢!

This is not correct 这是不对的

cv::DFT_INVERSE||DFT_COMPLEX_OUTPUT

, if you want combine binary values you should use "binary or": ,如果要合并二进制值,则应使用“ binary or”:

cv::DFT_INVERSE | DFT_COMPLEX_OUTPUT

or + operation 或+操作

cv::DFT_INVERSE + DFT_COMPLEX_OUTPUT

A || A || B - is logical or. B-是逻辑或。 A, B and result may be only true or false. A,B和结果只能是对或错。

A | A | B - is bitwise or. B-按位或。

You can also try DFT_SCALE flag. 您也可以尝试DFT_SCALE标志。

DFT_SCALE scales the result: divide it by the number of array elements. DFT_SCALE缩放结果:将其除以数组元素的数量。 Normally, it is combined with DFT_INVERSE. 通常,它与DFT_INVERSE结合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM