Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
320 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...