The Fourier Transform will decompose an image into its sinus and
cosines components. It will transform an image from its
spatial domain to its frequency domain. Mathematically
a two dimensional images Fourier transform is:
Here f is the image value in its spatial domain and F in its
frequency domain. The result of the transformation is complex numbers.
dft Performs a forward or inverse Discrete Fourier transform of a 1D or 2D floating-point array.
void dft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)
Parameters:
Parameters:
Sources:
http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#void%20dft%28InputArray%20src,%20OutputArray%20dst,%20int%20flags,%20int%20nonzeroRows%29
http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html
dft Performs a forward or inverse Discrete Fourier transform of a 1D or 2D floating-point array.
void dft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)
Parameters:
- src – input array that could be real or complex.
- dst – output array whose size and type depends on the flags .
- flags – transformation flags, representing a combination of the following values:
- DFT_INVERSE performs an inverse 1D or 2D transform instead of the default forward transform.
- DFT_SCALE scales the result: divide it by the number of array elements. Normally, it is combined with DFT_INVERSE.
- DFT_ROWS performs a forward or inverse transform of every individual row of the input matrix; this flag enables you to transform multiple vectors simultaneously and can be used to decrease the overhead (which is sometimes several times larger than the processing itself) to perform 3D and higher-dimensional transformations and so forth.
- DFT_COMPLEX_OUTPUT performs a forward transformation of 1D or 2D real array; the result, though being a complex array, has complex-conjugate symmetry (CCS, see the function description below for details), and such an array can be packed into a real array of the same size as input, which is the fastest option and which is what the function does by default; however, you may wish to get a full complex array (for simpler spectrum analysis, and so on) - pass the flag to enable the function to produce a full-size complex output array.
- DFT_REAL_OUTPUT performs an inverse transformation of a 1D or 2D complex array; the result is normally a complex array of the same size, however, if the input array has conjugate-complex symmetry (for example, it is a result of forward transformation with DFT_COMPLEX_OUTPUT flag), the output is a real array; while the function itself does not check whether the input is symmetrical or not, you can pass the flag and then the function will assume the symmetry and produce the real output array (note that when the input is packed into a real array and inverse transformation is executed, the function treats the input as a packed complex-conjugate symmetrical array, and the output will also be a real array).
- nonzeroRows – when the parameter is not zero, the function assumes that only the first nonzeroRows rows of the input array (DFT_INVERSE is not set) or only the first nonzeroRows of the output array (DFT_INVERSE is set) contain non-zeros, thus, the function can handle the rest of the rows more efficiently and save some time; this technique is very useful for calculating array cross-correlation or convolution using DFT.
Parameters:
- src – input floating-point real or complex array.
- dst – output array whose size and type depend on the flags.
- flags – operation flags (same as dft()).
- nonzeroRows – number of dst rows to process; the rest of the rows have undefined content
Example 1:
------------#include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace std; using namespace cv; int main() { // Read image from file // Make sure that the image is in grayscale Mat img = imread("lena.JPG",0); Mat planes[] = {Mat_<float>(img), Mat::zeros(img.size(), CV_32F)}; Mat complexI; //Complex plane to contain the DFT coefficients {[0]-Real,[1]-Img} merge(planes, 2, complexI); dft(complexI, complexI); // Applying DFT // Reconstructing original imae from the DFT coefficients Mat invDFT, invDFTcvt; idft(complexI, invDFT, DFT_SCALE | DFT_REAL_OUTPUT ); // Applying IDFT invDFT.convertTo(invDFTcvt, CV_8U); imshow("Output", invDFTcvt); //show the image imshow("Original Image", img); // Wait until user press some key waitKey(0); return 0; }------------
Example 2:
------------#include "opencv2/highgui/highgui.hpp" #include <iostream> using namespace std; using namespace cv; int main() { // Read image from file // Make sure that the image is in grayscale Mat img = imread("lena.JPG",0); Mat dftInput1, dftImage1, inverseDFT, inverseDFTconverted; img.convertTo(dftInput1, CV_32F); dft(dftInput1, dftImage1, DFT_COMPLEX_OUTPUT); // Applying DFT // Reconstructing original imae from the DFT coefficients idft(dftImage1, inverseDFT, DFT_SCALE | DFT_REAL_OUTPUT ); // Applying IDFT inverseDFT.convertTo(inverseDFTconverted, CV_8U); imshow("Output", inverseDFTconverted); //show the image imshow("Original Image", img); // Wait until user press some key waitKey(0); return 0; }------------
Sources:
http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#void%20dft%28InputArray%20src,%20OutputArray%20dst,%20int%20flags,%20int%20nonzeroRows%29
http://docs.opencv.org/doc/tutorials/core/discrete_fourier_transform/discrete_fourier_transform.html