MATLAB 中的 fft2 与 OpenCV C++ 中的 dft 速度比较

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

我想知道为什么 OpenCVC++ 中的 dft 函数对于二维矩阵比 fft2 慢很多。

以下C++代码来自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 代码:

mat1 = rand(5000,5000);
tic, a = fft2(mat1); toc

两种代码的结果相同;但是,C++ 代码花费了 1502 毫秒,而 MATLAB 代码花费了 660 毫秒。 OpenCV 中似乎缺少一些优化。我想知道如何加速 OpenCVcode。

我正在 Visual Studio 2015 年使用 OpenCV 2.4.10 和 MATLAB R2016a。计算机是 Windows 7、32 GB RAM、Intel Xeon 3.4 GHz。两项测试均在同一台机器上进行。

我找到了一堆 FFT 代码,但它们似乎很难应用于矩阵。有矩阵的简单解决方案吗?

OpenCV 的 FFT 实现可能不如 Matlab 的优化。
如果您需要 FFT 性能,请查看专门的 FFT 库,例如 FFTW.