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:
- The difference between DFT (at OpenCV) and FFT (Halcon)
- 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?