问题
I'm wondering why the dft
function in OpenCVC++ is a lot slower than fft2
for 2D matrices.
The following C++ code is from the documentation:
void fft2(const Mat in, Mat &complexI) {
Mat padded;
int m = getOptimalDFTSize(in.rows);
int n = getOptimalDFTSize(in.cols);
copyMakeBorder(in, padded, 0, m - in.rows, 0, n - in.cols, BORDER_CONSTANT, Scalar::all(0));
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
merge(planes, 2, complexI);
dft(complexI, complexI);
}
int main(){
Mat a(5000, 5000, CV_32F);
randn(a, 0, 1);
Mat res;
clock_t start = clock();
fft2(a,res);
cout << clock() - start;
}
MATLAB code:
mat1 = rand(5000,5000);
tic, a = fft2(mat1); toc
The result for both codes is same; however, the C++ code is taking 1502 ms whereas the MATLAB code is taking 660ms. It seems some optimizations are missing in OpenCV. I'm wondering how I can speed up the OpenCVcode.
I'm working on Visual Studio 2015 using OpenCV 2.4.10 and MATLAB R2016a. The computer is Windows 7, 32 GB RAM, Intel Xeon 3.4 GHz. Both tests were conducted on the same machine.
I found bunch of FFT codes but they seem hard to apply to matrices. Is there an easy solution for matrices?
回答1:
OpenCV's FFT implementation is probably not as optimized as Matlab's.
If you FFT performance is what you require, then take a look at specialized FFT libraries like FFTW.
来源:https://stackoverflow.com/questions/37900007/fft2-in-matlab-vs-dft-in-opencv-c-speed-comparison