Matlab在OpenCV中的ifft2功能

时间:2015-07-14 09:36:17

标签: c++ matlab opencv fft ifft

我已经在互联网上搜索过很多但仍未找到解决方案。我想要的是将Matlab的ifft2函数转换为OpenCV以获得相同的结果。例如,如果我有一个10x10矩阵A

A =

    11    22    33     1     1     1     1     1     1     1
    44    55    66     1     1     1     1     1     1     1
    77    88    99     1     1     1     1     1     1     1
   100   111   122     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1

然后在Matlab中,调用ifft2(A)给了我这个:

ans =

   9.1600 + 0.0000i   5.4570 + 4.6041i   0.5640 + 4.4443i  -1.1170 + 0.7295i   1.0560 - 1.4066i   2.7200 + 0.0000i   1.0560 + 1.4066i  -1.1170 - 0.7295i   0.5640 - 4.4443i   5.4570 - 4.6041i
   1.7274 + 6.5730i  -2.5500 + 5.5266i  -3.6046 + 1.6030i  -1.0629 - 0.6619i   1.2068 + 0.5078i   0.5758 + 2.1910i  -0.9060 + 1.3047i   0.2070 - 0.8463i   3.4605 - 0.4030i   4.7141 + 3.3760i
  -3.6507 + 1.1351i  -3.2357 - 1.1758i  -1.1907 - 1.8927i   0.1292 - 0.6560i  -0.3480 + 0.5913i  -1.2169 + 0.3784i  -0.8219 - 0.3575i   0.2811 + 0.1882i   0.0968 + 1.8925i  -1.8722 + 2.6341i
   0.6876 - 1.9331i   1.4480 - 0.9227i   0.9832 + 0.1199i   0.0550 + 0.1593i  -0.1955 - 0.4607i   0.2292 - 0.6444i   0.3196 - 0.1496i  -0.3841 + 0.0821i  -1.0290 - 0.6749i  -0.5821 - 1.7727i
   1.1457 + 1.6084i  -0.1392 + 1.7632i  -0.8331 + 0.7911i  -0.3628 - 0.0938i   0.3857 + 0.0000i   0.3819 + 0.5361i  -0.1268 + 0.4468i  -0.0493 - 0.2667i   0.8930 - 0.4889i   1.6339 + 0.4186i
  -1.6800 + 0.0000i  -1.1861 - 0.8618i  -0.2800 - 0.8618i   0.0661 - 0.2034i  -0.2800 + 0.2034i  -0.5600 + 0.0000i  -0.2800 - 0.2034i   0.0661 + 0.2034i  -0.2800 + 0.8618i  -1.1861 + 0.8618i
   1.1457 - 1.6084i   1.6339 - 0.4186i   0.8930 + 0.4889i  -0.0493 + 0.2667i  -0.1268 - 0.4468i   0.3819 - 0.5361i   0.3857 - 0.0000i  -0.3628 + 0.0938i  -0.8331 - 0.7911i  -0.1392 - 1.7632i
   0.6876 + 1.9331i  -0.5821 + 1.7727i  -1.0290 + 0.6749i  -0.3841 - 0.0821i   0.3196 + 0.1496i   0.2292 + 0.6444i  -0.1955 + 0.4607i   0.0550 - 0.1593i   0.9832 - 0.1199i   1.4480 + 0.9227i
  -3.6507 - 1.1351i  -1.8722 - 2.6341i   0.0968 - 1.8925i   0.2811 - 0.1882i  -0.8219 + 0.3575i  -1.2169 - 0.3784i  -0.3480 - 0.5913i   0.1292 + 0.6560i  -1.1907 + 1.8927i  -3.2357 + 1.1758i
   1.7274 - 6.5730i   4.7141 - 3.3760i   3.4605 + 0.4030i   0.2070 + 0.8463i  -0.9060 - 1.3047i   0.5758 - 2.1910i   1.2068 - 0.5078i  -1.0629 + 0.6619i  -3.6046 - 1.6030i  -2.5500 - 5.5266i

在OpenCV中,我编写了以下代码(感谢互联网),确实获得了与Matlab相同的结果

cv::Mat inverseDFT;
cv::Mat planes[] = { cv::Mat_<float>(input), cv::Mat::zeros(input.size(), CV_32F) };
cv::Mat complex;
cv::merge(planes, 2, complex);
cv::idft(complex, inverseDFT, cv::DFT_SCALE | cv::DFT_INVERSE);

到目前为止这很好。 但是,当我使用相同类型和值的另一个大小为82x82的矩阵时,Matlab的结果与OpenCV的结果非常不同。这是Matlab输出(前两行):

1.0e+05 * 
[-0.0000   -9.6761   -4.6365    1.8979    1.4972   -0.0025    0.1037   -0.0919    0.3140    0.0032    0.0195    0.0057   -0.0002    0.0147   -0.0023    0.0088    0.0045   -0.0023    0.0135;
-0.6180    4.6026    1.9042   -1.0747   -0.5034   -0.0088   -0.0466    0.0479   -0.1339   -0.0019   -0.0078   -0.0019   -0.0031   -0.0098    0.0009   -0.0038   -0.0053    0.0034   -0.0070;
....]

这是OpenCV结果(前两行):

[15.8347    148.3   15.5297 148.068 15.3074 147.476 15.1266 146.854 14.9336 146.121 14.649  145.361 14.4584 144.357 14.1337 143.344 13.8938 142.052 13.4711 140.892 12.9521 139.762 12.3772 138.7   11.7153 137.773 11.149  136.736 10.6333 136.07  10.0025 135.68  9.35163 135.744 8.37026 136.085 7.51313 136.597 6.65663 137.297 5.84039 138.144 5.08981 138.964 4.2696  139.838 3.93125 140.847 3.83195 141.894 4.0224  142.883 4.54194 143.872 5.35709 144.933 6.18112 145.637 6.97564 146.306 7.91474 146.833 8.90979 147.268 9.90416 147.69  10.7855 147.929 11.9145 148.177 13.3188 148.587 14.5545 148.747 15.3029 148.96  15.8501 149.022 16.1713 149.002 16.0789 148.678 ; 
83.1579 -72.1501    83.2033 -72.3991    83.0443 -72.9689    83.028  -73.201 82.9732 -73.2787    82.9527 -73.2777    83.039  -73.2688    83.2974 -73.331 83.7781 -73.6298    84.5012 -73.8774    85.2545 -74.02  86.2663 -74.1823    87.4866 -74.3593    88.8467 -74.474 90.3635 -74.5438    91.6828 -74.5036    92.9061 -73.9815    93.1934 -73.0743    93.4254 -72.1992    93.1917 -71.2198    92.45   -70.201 91.1874 -69.4783    89.0805 -68.5612    87.2752 -67.461 85.2434 -66.3476    83.1033 -65.316 80.9829 -64.4404    79.0108 -63.7758    77.3056 -63.3485    75.7612 -63.1322    74.7189 -63.3549    74.1633 -63.9313    74.1057 -64.8886    74.2431 -66.0125    74.9995 -66.9026    76.9265 -67.4861    78.861  -68.5714    80.1996 -69.2898    81.3412 -70.1617    82.2211 -70.9372    82.7735 -71.4485;
    ....]

造成这种差异的原因是什么?有人可以帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:0)

实际上Matlab中的fft是一个离散傅立叶变换, 我发现可以通过OpenCV的dct

获得相同的输出