Xiaotian Hu
Xiaotian Hu

Reputation: 33

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. I load the same image, use fft2() and dft() to do the transform and measure the time they consumed. 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. How come? The OpenCV version I used is 2.4.8 while the Matlab version is 2013 a. Here is my codes for testing

Matlab:

tic
X1 = fft2(im);
toc

OpenCV in C++:

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

Upvotes: 1

Views: 1182

Answers (2)

smttsp
smttsp

Reputation: 4191

I've asked this and similar questions for very long time fft vs dft and Matlab vs c++. The answer I found is,

  1. Matlab has some built-in software such as MKL, Lapack and BLAS.
  2. They use c or Fortran libraries behind the scene.
  3. They use the best implementations. For example, fft2 in Matlab is FFTW based. (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.
  2. You are not using the DFT as suggested, you can improve the speed by getting the optimal dimensions. 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.

Upvotes: 0

In general fft is a fast implementation of 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. W  is a complex NxN matrix with entiries W_k,n=exp(-2pikn/N), where 0 < k , n < N.

FFT is a collection of algorithms for fast computation of the DFT. Typically the number of operations required by the FFT is on the order of 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.

Upvotes: 1

Related Questions