2013-11-14 110 views
0

我想这个简单的Matlab代码转换为C++与OpenCV的:经与FFT和IFFT困难的OpenCV

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

这意味着采取矩阵“输出”的FFT,逐个元素相乘与矩阵“gf”,然后采取的话,然后采取的幅度。

我想下面简单的代码:

Mat planes[] = {Mat_<float>(output), Mat::zeros(output.size(), CV_32F)}; 
    Mat complexI; 
    merge(planes, 2, complexI);   // Add to the expanded another plane with zeros 

    dft(complexI, complexI,cv::DFT_SCALE); 
    for (int i=0;i<complexI.rows;i++){ 
     for (int j=0;j<complexI.cols;j++){ 
      complexI.at<float>(i,j)*=gf.at<float>(i,j); 
     } 
    } 

    //now the inverse transform 
    dft(complexI,complexI,cv::DFT_INVERSE); 
    split(complexI, 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)); 
    } 
} 

这很简单 - 我施加FFT,得到一个复杂的结果。然后将元素与gf相乘,然后进行逆变换,将结果分解为两个矩阵 - 实数和虚数 - 然后取其大小。

然而,即使它很简单,我没有看到任何错误,结果是非常不同,那么我在Matlab中得到。太大而不能用舍入误差来解释。

有人可以请我指出我可能做错了什么?

我使用Matlab2013a,OpenCV的2.4.5用VS 2012在Windows 7

在此先感谢,

吉尔。

编辑:我加了sqrt的结果,但仍然有很大的差异。

+0

如何定义过滤器? –

回答

1

在MatLAB版本中,您从结果和OpenCV中取平方根 - 不是。你检查过了吗?

+0

在openCV中,我取平方根(幅度取平方根)。 请看这里: http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html – GilLevi

+0

我的意思是在MatLAB中,你需要_additional_平方根。 MatLAB中的函数abs与复杂数据的作用与OpenCV中的幅度相同http://www.mathworks.com/help/matlab/ref/abs.html –

+0

谢谢,我添加了sqrt,但结果仍然非常不同。 – GilLevi

1

好吧,马上关闭我看到您的过滤问题。我不确定那个回路会做什么,但要进行频率过滤,您应该使用功能mulSpectrums

此外,如果你想采取幅度的sqrt,你可以使用OpenCV的sqrt函数,而不必通过在运营商。

+0

谢谢,我会尝试根据您的意见修改我的代码。 – GilLevi

+0

没问题,希望对您有所帮助 –

1

如果gf也是复数矩阵,也就是CV_64FC2/CV_32FC2,那么您可能需要使用mulSpectrums。否则,如果你想自己将它们相乘,那么你应该使用std :: complex来访问这些复杂的值。 std :: complex将为您执行复杂的操作。

for (int i=0;i<complexI.rows;i++){ 
     for (int j=0;j<complexI.cols;j++){ 
      complexI.at<complex<double>>(i,j)*=gf.at<complex<double>>(i,j); 
     } 
    } 
+0

感谢您的帮助! – GilLevi