简体   繁体   English

为什么Matlab fft2比OpenCV dft快得多

[英]why Matlab fft2 is much faster than OpenCV dft

I just did test to compare the speed bewteen the dft function of OpenCV and fft2 in Matlab. 我只是在Matlab中进行了测试,以比较OpenCV和ftft2的dft函数之间的速度。 I load the same image, use fft2() and dft() to do the transform and measure the time they consumed. 我加载相同的图像,使用fft2()和dft()进行转换并测量它们消耗的时间。 I found that for the image the dft() costed over 2 second in the win32 release version while the fft2() only took round 0.2s. 我发现对于图像,dft()在win32发行版中花费了2秒钟以上,而fft2()仅花费了约0.2s。 How come? 怎么会? The OpenCV version I used is 2.4.8 while the Matlab version is 2013 a. 我使用的OpenCV版本是2.4.8,而Matlab版本是2013 a。 Here is my codes for testing 这是我的测试代码

Matlab: Matlab的:

tic
X1 = fft2(im);
toc

OpenCV in C++: C ++中的OpenCV:

start1 = clock();
dft(src,src,DFT_COMPLEX_OUTPUT);
end1 = clock();
cout<<(double)(end1 - start1)/CLOCKS_PER_SEC<<endl;

In general fft is a fast implementation of dft. 通常,ftf是dft的快速实现。

DFT is a linear transform which takes as input a complex signal x of length N and gives as output a complex signal X of length N, X=Wx. DFT是线性变换,其将长度为N的复数信号x作为输入,并且将长度为N的复数信号X作为输出给出,X = Wx。 W is a complex NxN matrix with entiries W_k,n=exp(-2pi k n/N), where 0 < k , n < N. W是一个复数NxN矩阵,具有W_k,n = exp(-2pi k n / N)的项,其中0 <k,n <N.

FFT is a collection of algorithms for fast computation of the DFT. FFT是用于快速计算DFT的算法的集合。 Typically the number of operations required by the FFT is on the order of N*logN. 通常,FFT所需的运算数量约为N * logN。 The most famous FFT algorithms are for the case that N is a power of 2, but there are FFT for prime orders and for different other factorizations. 最著名的FFT算法是针对N是2的幂的情况,但是对于素数阶和不同的其他因式分解有FFT。

I've asked this and similar questions for very long time fft vs dft and Matlab vs c++ . 我已经问过这个问题和类似的问题很长时间了fft vs dftMatlab vs c ++ The answer I found is, 我找到的答案是,

  1. Matlab has some built-in software such as MKL, Lapack and BLAS. Matlab具有一些内置软件,例如MKL,Lapack和BLAS。
  2. They use c or Fortran libraries behind the scene. 他们在后台使用c或Fortran库。
  3. They use the best implementations. 他们使用最佳的实现。 For example, fft2 in Matlab is FFTW based. 例如,Matlab中的fft2基于FFTW。 (Fastest Fourier Transform in the West) (西方最快的傅立叶变换)
  4. They are always improving. 他们一直在进步。 Newer versions are noticeably faster than older ones in for some functions. 在某些功能上,较新版本明显比旧版本快。

On the other hand, 另一方面,

  1. You are not using the latest version of OpenCV which should have some effect on the performance. 您没有使用最新版本的OpenCV,它应该会对性能产生一定的影响。
  2. You are not using the DFT as suggested, you can improve the speed by getting the optimal dimensions . 您没有按照建议使用DFT,可以通过获取最佳尺寸来提高速度。 If your image dimensions are not optimal, it may significantly increase the running time. 如果图像尺寸不是最佳尺寸,则可能会大大增加运行时间。

Final note: it is not recommended to use tic, toc , instead use timeit . 最后说明:不建议使用tic, toc ,而是使用timeit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM