Ask Your Question

OpenCV Fourier Transform complex output issue

asked 2018-10-12 15:16:56 -0600

t3rb3d gravatar image

I try to do some Fourier analysis, but in the OpenCV cv::dft function the cv::DFT_COMPLEX_OUTPUT seems not working. I do the following:

// Calculate Fourier transform for each time signal
cv::Mat hf, hf_raw, h, h_raw;
cv::Mat Fp(Pt.size[0], Pt.size[1], CV_64FC2);
cv::Mat Fz(Pt.size[0], Pt.size[1], CV_64FC2);

cv::dft(Pt.t(), Fp, cv::DFT_ROWS, cv::DFT_COMPLEX_OUTPUT);
cv::dft(Zt.t(), Fz, cv::DFT_ROWS, cv::DFT_COMPLEX_OUTPUT);

// [DEBUG]
// Fp and Fz has to be (K*4 x WINDOW_LENGTH) size in the current case (24 x 256)
std::cout << "Fp.size: " << Fp.size << std::endl;
// -> OK
// Fp elements should be complx
std::cout << "Fp.type(): " << Fp.type() << std::endl;
// -> Lots of zero at the end!!! -> With cv::DFT_REAL_OUTPUT there are no zeros

cv::Mat nom, denom, W;
cv::mulSpectrums(Fp, Fp, nom, cv::DFT_ROWS, true);
cv::mulSpectrums(Fz, Fz, denom, cv::DFT_ROWS, true);

I declare Fp and Fz to be two channeled in order to hold the real and complex values (on which the cv::mulSpectrums function can operate). But Fp.type() results in 6 which means the type of the output matrix is CV_64FC1.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-13 01:24:50 -0600

berak gravatar image

updated 2018-10-13 01:27:49 -0600

please see docs , your cv::DFT_COMPLEX_OUTPUT flag went into the wrong slot. it should be:

Mat Fp, Pt(256,128,CV_32F); // example input
cv::dft(Pt.t(), Fp, cv::DFT_ROWS | cv::DFT_COMPLEX_OUTPUT); //you need to "or" the flags

cout << Fp.size() << " " << Fp.channels() << " " << Fp.depth() << endl;
// [256 x 128] 2 5

also, please do NOT preallocate output Mat's like Fp and Zp. they will most likely be overwritten anyway, and you only fool yourself making assumptions about size/type here.

edit flag offensive delete link more


Thank you for your reply! It is a useful information that the flags should be given with the 'or' operator. I didn't know about that, I am not an OpenCV expert...

t3rb3d gravatar imaget3rb3d ( 2018-10-13 02:21:32 -0600 )edit

Question Tools

1 follower


Asked: 2018-10-12 15:16:56 -0600

Seen: 649 times

Last updated: Oct 13 '18