matlab fft2的作用,为什么Matlab fft2比OpenCV dft快得多(why Matlab fft2 is much faster than OpenCV dft)...

为什么Matlab fft2比OpenCV dft快得多(why Matlab fft2 is much faster than OpenCV dft)

我只是测试比较OpenCV的dft函数和Matlab中的fft2的速度。 我加载相同的图像,使用fft2()和dft()进行转换并测量它们消耗的时间。 我发现,对于图像,dft()在win32发布版本中的成本超过2秒,而fft2()只花了0.2秒。 怎么会? 我使用的OpenCV版本是2.4.8而Matlab版本是2013年。 这是我的测试代码

Matlab的:

tic

X1 = fft2(im);

toc

C ++中的OpenCV:

start1 = clock();

dft(src,src,DFT_COMPLEX_OUTPUT);

end1 = clock();

cout<

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<

原文:https://stackoverflow.com/questions/42669750

更新时间:2020-03-30 17:03

最满意答案

很长一段时间fft vs dft和Matlab vs c ++我都问过这个和类似的问题。 我找到的答案是,

Matlab有一些内置软件,如MKL,Lapack和BLAS。

他们在场景后面使用c或Fortran库。

他们使用最好的实现。 例如,Matlab中的fft2是基于FFTW的。 (西方最快的傅立叶变换)

他们总是在改进。 对于某些功能,较新版本明显比旧版本快。

另一方面,

您没有使用最新版本的OpenCV,这会对性能产生一些影响。

您没有按照建议使用DFT,您可以通过获得最佳尺寸来提高速度。 如果图像尺寸不是最佳,则可能会显着延长运行时间。

最后注意:不建议使用tic, toc ,而是使用timeit 。

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

Matlab has some built-in software such as MKL, Lapack and BLAS.

They use c or Fortran libraries behind the scene.

They use the best implementations. For example, fft2 in Matlab is FFTW based. (Fastest Fourier Transform in the West)

They are always improving. Newer versions are noticeably faster than older ones in for some functions.

On the other hand,

You are not using the latest version of OpenCV which should have some effect on the performance.

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.

2017-05-23

相关问答

我假设这是对这个问题的跟进,它描述了你对ImageJ的使用,它提供了方向和频率参数的直接读数。 假设你有一个特定的像素A(i,j) ,那么可以使用以下方法获得像素/周期的方向和频率(由ImageJ类似地获得): center = size(A)/2 + 1;

dx = (j-center(2))/size(A,2);

dy = (center(1)-i-1)/size(A,1);

direction = atan2(dy, dx); % in radians

frequency = 1/sqrt

...

这看起来像你需要的: FEX 虽然我不确定你为什么不使用2D信息。 This looks like what you need: FEX Although I am not sure why you wouldn't just use the 2D information.

使用find确定图像中最大值出现的位置。 然而,问题是忽略DC值,这很可能是目前为止你频谱中最大的组成部分。 因此,您应该做的是找到最大值,该值应该是当前的DC值,并将其设置为NaN 。 完成此操作后,下一个最大值就是您要搜索的问题。 因此,首先执行此操作: idx = find(img_shift == max(img_shift(:)));

img_shift(idx) = NaN;

如果您将此处的最大值设置为NaN ,则下次调用find ,它将忽略此位置并在搜索最大值时跳过此位置。 现在,

...

您熟悉DFT的矩阵形式吗? 看看这里: http : //en.wikipedia.org/wiki/DFT_matrix 你可以做类似的事情来获得2D DFT的矩阵形式。 你需要转换矩阵。 第一个是N-by-N DFT矩阵,它在f的列上运行,如上面的链接所述。 接下来,您需要另一个M-byM DFT矩阵,对f的行进行操作。 最后,你改变了信号 F = Wm * f * Wn;

没有任何循环。 注意,DFT矩阵也可以通过使用类似的东西来构造而没有循环 (1:M)*((1:M)')

Are yo

...

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.

这将是一个数值问题。 值在1e-15范围内,而信号的DFT值在1e+02范围内。 在进行进一步处理时,这很可能不会导致任何错误。 您可以通过计算DFT和MATLAB fft函数之间的总平方误差 y = fft(x);

yh = Discrete_Fourier_Transform(x);

sum(abs(yh - y).^2)

ans =

3.1327e-20

这基本上是零。 因此,我的结论是:你的DFT功能运行得很好。 只需一句小话:您可以轻松地对DFT进行矢量化。 n = 0:1:

...

原因在于两个不同的信号肯定会给出两个不同的频谱。 看看下面的代码,你会发现你实际给出的dft算法的输入是sampledI+jsampledQ 。 因此,您在这里所做的不仅仅是将您的原始信号分解为In-phase and quadrature components ,而是在这里进行Hilbert transform - 将real信号转换为complex信号。 cI = cos(2*pi*w*sampleIdx/fftSize); %correlation cos

cQ = -sin(2*p

...

你不需要HP=fft2(hp); , hp已经在频域。 只需删除该行,然后使用 HP=ifftshift(hp);

F=fft2(f);

G=HP.*F;

g=real(ifft2(G));

imshow(g)

You don't need HP=fft2(hp);, hp is already at the frequency domain. Just remove that line, and use HP=ifftshift(hp);

F=fft2(f);

G=HP.*F;

...

内置函数的Matlab附带了mkl和opencv的功能。 因此,如果两者中存在两个完全相同的函数,则matlab可能比opencv更快(更多)。 我试图在一个大矩阵上进行伪逆,并且matlab击败所有东西(openblas,Armadillo,自我集成mkl等)至少2次。 然后我就停止弄清楚为什么只是将数据加载到matlab中并让它做到这一点。 opencv是迄今为止最慢的。 在opencv中的10000x10000矩阵上尝试矩阵乘法。 我的笔记本电脑花了10分钟。 Matlab用了1分钟。 Ma

...

很长一段时间fft vs dft和Matlab vs c ++我都问过这个和类似的问题。 我找到的答案是, Matlab有一些内置软件,如MKL,Lapack和BLAS。 他们在场景后面使用c或Fortran库。 他们使用最好的实现。 例如,Matlab中的fft2是基于FFTW的。 (西方最快的傅立叶变换) 他们总是在改进。 对于某些功能,较新版本明显比旧版本快。 另一方面, 您没有使用最新版本的OpenCV,这会对性能产生一些影响。 您没有按照建议使用DFT,您可以通过获得最佳尺寸来提高速度。

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值