fft2 in MATLAB vs dft in OpenCV C++ speed comparison

你说的曾经没有我的故事 提交于 2019-12-09 19:39:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!