简体   繁体   中英

FFT and IFFT - difference between results in Matlab and openCV

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

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.

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. 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.

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?

Thanks in advance!

This is not correct

cv::DFT_INVERSE||DFT_COMPLEX_OUTPUT

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

cv::DFT_INVERSE | DFT_COMPLEX_OUTPUT

or + operation

cv::DFT_INVERSE + DFT_COMPLEX_OUTPUT

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

A | B - is bitwise or.

You can also try DFT_SCALE flag.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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