Ask Your Question

Revision history [back]

Having difficulty with FFT and IFFT in 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 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];

It's very simple - I'm applying the fft, getting a complex results. Then multiplying element by element with gf, then taking the inverse transform, splitting the result to two matrices - real and imaginary- then taking the magnitude of that.

However, even though it's very simple and I don't see any errors, the results are very different then what I get in Matlab. Far too large to be explained by rounding errors.

Can someone please point me to what I might be doing wrong?

I'm using Matlab2013a, openCV 2.4.5 with VS 2012 on windows 7.

Thanks in advance,

Gil.

Having difficulty with FFT and IFFT in 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 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));
    }
}

It's very simple - I'm applying the fft, getting a complex results. Then multiplying element by element with gf, then taking the inverse transform, splitting the result to two matrices - real and imaginary- then taking the magnitude of that.

However, even though it's very simple and I don't see any errors, the results are very different then what I get in Matlab. Far too large to be explained by rounding errors.

Can someone please point me to what I might be doing wrong?

I'm using Matlab2013a, openCV 2.4.5 with VS 2012 on windows 7.

Thanks in advance,

Gil.