简体   繁体   中英

Inverse FFT in OpenCV gives different result than Matlab

Using OpenCV, I want to get the same result of Matlab's ifft2 function. In Matlab I get:

A = [1 2 0; 4 5 0; 7 8 9; 10 11 12];
inverse = ifft2(A);

inverse =

   5.7500 + 0.0000i  -0.1250 + 0.3608i  -0.1250 - 0.3608i
  -1.7500 - 2.0000i  -0.3080 + 0.4665i   0.5580 + 0.0335i
  -1.2500 + 0.0000i  -0.1250 - 0.2165i  -0.1250 + 0.2165i
  -1.7500 + 2.0000i   0.5580 - 0.0335i  -0.3080 - 0.4665i

In OpenCV, when I do:

cv::Mat paddedA;
int m = cv::getOptimalDFTSize(A.rows);
int n = cv::getOptimalDFTSize(A.cols);

cv::copyMakeBorder(A, paddedA, 0, m - A.rows, 0, n - A.cols, cv::BORDER_CONSTANT, cv::Scalar::all(0));
cv::Mat planes[] = { cv::Mat_<float>(paddedA), cv::Mat::zeros(paddedA.size(), CV_32F) };
cv::Mat complexA;
cv::merge(planes, 2, complexA);

cv::Mat inverse;
cv::idft(complexA, inverse, cv::DFT_SCALE | cv::DFT_COMPLEX_OUTPUT);

This gives me:

inverse = [1, -0, 2, -0, 0, -0;
     4, -0, 5, -0, 0, -0;
     7, -0, 8, -8, 9, 0;
     10, -0, 11, -0, 12, -0]

And the type of A is CV_32F . How can I get the same result as that of Matlab?

You are using two different functions. FFT is optimized version of DFT. You should use step function in matlab then compare the results.

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