DFT和FFT(幅度)结果之间的差异 [英] Diferences between DFT and FFT (magnitude) results

查看:551
本文介绍了DFT和FFT(幅度)结果之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在OpenCV中获取图像的DFT.

I aim to get the DFT of an image in OpenCV.

使用dft函数,我可以对其进行计算,然后通过计算其幅值对其进行绘制(然后,应用对数并最终对其进行归一化,以绘制0到1之间的值).

Using dft function, I'm able to calculate it, and then paint it by calculating its magnitude (then, apply the log and finally normalize it in order to paint values between 0 and 1).

对于下面的图像,我的结果是我显示给您的结果(为了使图像中心的频率降低而进行了交换):

My result is, for the following image, the result I show you (with swap in order to have lower frequencies in the center of the image):

但是,如果将其与使用其他工具(例如 Halcon )获得的结果进行比较,则似乎不正确对我来说,这似乎确实有很高的价值(我指的是OpenCV DFT幅度):

However, if I compare it to the result I obtain using other tools like Halcon, It seems incorrect to my since It seems to have really "high" values (the OpenCV DFT magnitude I mean):

我认为可能是由于以下原因:

I thought it might be for these reasons:

  1. DFT (在OpenCV)和 FFT (Halcon)之间的区别
  2. 我正在执行的操作为了显示OpenCV中的幅度.
  1. The difference between DFT (at OpenCV) and FFT (Halcon)
  2. The operations I'm performing in order to show the magnitude in OpenCV.

第一个问题是我很难分析这个问题,并且OpenCV不具有FFT功能,而Halcon不具有DFT功能(如果我我当然没错),所以我无法直接进行比较.

The first one have as problem that it's quite hard for me to analyze, and OpenCV doesn't have a FFT function, as well as Halcon doesn't have a DFT function (if I'm not wrong of course), so I can't compare it directly.

第二个是我工作时间最多的时间,但是我仍然找不到原因.

The second one is in which I've been working the most time, but I still don't find the reason if it's there.

我正在使用代码来绘制img的大小(这是我的DFT图像):

There's the code I'm using to paint the magnitude of img (which is my DFT image):

// 1.- To split the image in Re | Im values
Mat planes[] = {Mat_<float>(img), Mat::zeros(img.size(), CV_32F)};

// 2.- To magnitude + phase
split(img, planes);

// Calculate magnitude. I overwrite it, I know, but this is inside a function so it will be never used again, doesn't matter
magnitude(planes[0], planes[1], planes[0]);

// Magnitude Mat
Mat magI = planes[0];

// 3.- We add 1 to all them in order to perform the log
magI += Scalar::all(1);                    // switch to logarithmic scale
log(magI, magI);

// 4.- Swap the quadrants to center frequency
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
int cx = magI.cols/2;
int cy = magI.rows/2;

Mat q0(magI, Rect(0, 0, cx, cy));   // Top-Left - Create a ROI per quadrant
Mat q1(magI, Rect(cx, 0, cx, cy));  // Top-Right
Mat q2(magI, Rect(0, cy, cx, cy));  // Bottom-Left
Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right

// swap quadrants (Top-Left with Bottom-Right)
Mat tmp;
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);

// swap quadrant (Top-Right with Bottom-Left)
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);

// 5.- Normalize
// Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).
normalize(magI, magI, 0, 1, CV_MINMAX); 

// Paint it
imshow( "Magnitud DFT", magI);

总结一下:关于为什么我在这两个量级之间有这种差异的任何想法?

So summarizing: any idea about why do I have this difference between these two magnitudes?

推荐答案

我将我的评论总结成一个答案.

I'll summarize my comments into an answer.

当人们想到进行傅立叶变换以在逆域中工作时,假定进行逆变换将返回相同的函数/向量/任何东西.换句话说,我们假设

When one thinks of doing a Fourier transform to work in the inverse domain, the assumption is that doing the inverse transform will return the same function/vector/whatever. In other words, we assume

许多程序和库(例如Mathematica,Matlab/octave,本征/不受支持/FFT 等).但是,对于许多库( FFTW

This is the case with many programs and libraries (e.g. Mathematica, Matlab/octave, Eigen/unsupported/FFT, etc.). However, with many libraries (FFTW, KissFFT, etc.) this is not the case and there tends to be a scale

其中,s通常是数组中具有某种幂次幂的元素(m)的数量(如果在变换和逆向中不按不匹配的方式缩放,则应为1).这样做是为了避免对所有m元素乘以比例进行迭代,该比例通常为

where s is usually the number of elements (m) in the array to the power of something (should be 1 if not scaled in a mismatched fashion in both the transform and the inverse). This is done in order to refrain from iterating over all m elements multiplying by a scale, which is often not important.

也就是说,在逆域中查看比例时, 缩放转换的各种库都可以自由使用不同的比例进行转换和逆转换.变换/逆的常见缩放对包括{m^-1m}和{m^-0.5m^0.5}.因此,当比较来自不同库的结果时,我们应该准备好m(由m^-1缩放与未缩放)、、 m^0.5(由m^-0.5缩放与未缩放和由m^-1缩放)的因子.与使用m^-0.5进行缩放的比例),或者如果使用其他缩放因子,甚至可以使用其他比例进行缩放.

That being said, when looking at the scale in the inverse domain, various libraries that do scale the transforms have the liberty to use different scales for the transform and inverse transform. Common scaling pairs for the transform/inverse include {m^-1, m} and {m^-0.5, m^0.5}. Therefore, when comparing results from different libraries, we should be prepared to factors of m (scaled by m^-1 vs. not scaled), m^0.5 (scaled by m^-0.5 vs. not scaled and scaled by m^-1 vs. scaled by m^-0.5) or even other scales if other scaling factors were used.

注意:此缩放因子与规范化数组无关,因此所有值均为[0,1]或数组的范数等于1.

Note: This scaling factor is not related to normalizing an array, such that all values are [0,1] or that the norm of the array is equal to 1.

这篇关于DFT和FFT(幅度)结果之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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