MATLAB中的fft2与OpenCV C ++的dft速度比较 [英] fft2 in MATLAB vs dft in OpenCV C++ speed comparison

查看:197
本文介绍了MATLAB中的fft2与OpenCV C ++的dft速度比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么对于2D矩阵,OpenCVC ++中的dft函数比fft2慢很多.

I'm wondering why the dft function in OpenCVC++ is a lot slower than fft2 for 2D matrices.

以下C ++代码来自文档:

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代码:

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

两个代码的结果相同;但是,C ++代码需要1502毫秒,而MATLAB代码则需要660毫秒.似乎OpenCV中缺少一些优化.我想知道如何加快OpenCVcode的速度.

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.

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

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.

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

I found bunch of FFT codes but they seem hard to apply to matrices. Is there an easy solution for matrices?

推荐答案

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

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.

这篇关于MATLAB中的fft2与OpenCV C ++的dft速度比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆