Diferences between DFT and FFT (magnitude) results

荒凉一梦 提交于 2019-12-01 05:07:24

问题


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

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):

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. The difference between DFT (at OpenCV) and FFT (Halcon)
  2. The operations I'm performing in order to show the magnitude in OpenCV.

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.

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?


回答1:


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

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

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.

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.

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.



来源:https://stackoverflow.com/questions/30571641/diferences-between-dft-and-fft-magnitude-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!