Discrete Fourier Transform

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:
F(k,l) = \displaystyle\sum\limits_{i=0}^{N-1}\sum\limits_{j=0}^{N-1} f(i,j)e^{-i2\pi(\frac{ki}{N}+\frac{lj}{N})}

e^{ix} = \cos{x} + i\sin {x}

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:
  • 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.
void idft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)

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

7 comments:

  1. Doesn't work—neither example. Here's the error for example #2:

    2015-04-21 17:46:01.085 OpenCVCam[1289:162608] camera available: YES
    2015-04-21 17:46:02.065 OpenCVCam[1289:162608] [Camera] device connected? YES
    2015-04-21 17:46:02.066 OpenCVCam[1289:162608] [Camera] device position back
    2015-04-21 17:46:02.076 OpenCVCam[1289:162608] WARNING: -[ isVideoMinFrameDurationSupported] is deprecated. Please use AVCaptureDevice activeFormat.videoSupportedFrameRateRanges
    2015-04-21 17:46:02.077 OpenCVCam[1289:162608] WARNING: -[ setVideoMinFrameDuration:] is deprecated. Please use AVCaptureDevice setActiveVideoMinFrameDuration
    2015-04-21 17:46:02.078 OpenCVCam[1289:162608] WARNING: -[ isVideoMaxFrameDurationSupported] is deprecated. Please use AVCaptureDevice activeFormat.videoSupportedFrameRateRanges
    2015-04-21 17:46:02.079 OpenCVCam[1289:162608] WARNING: -[ setVideoMaxFrameDuration:] is deprecated. Please use AVCaptureDevice setActiveVideoMaxFrameDuration
    2015-04-21 17:46:02.082 OpenCVCam[1289:162608] layout preview layer
    2015-04-21 17:46:02.084 OpenCVCam[1289:162608] [Camera] created AVCaptureVideoDataOutput at 30 FPS
    OpenCV Error: Assertion failed (type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2) in dft, file /Users/build/test/opencv/modules/core/src/dxt.cpp, line 2466
    libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/build/test/opencv/modules/core/src/dxt.cpp:2466: error: (-215) type == CV_32FC1 || type == CV_32FC2 || type == CV_64FC1 || type == CV_64FC2 in function dft

    ReplyDelete
  2. Both examples here show idft(DFT_REAL_OUTPUT). I need the phase information of the IDFT. So I used the DFT_COMPLEX_OUTPUT. But the phase information I receive is a lot different than the one I get from MATLAB. Can anyone help please?

    ReplyDelete
  3. What are the drawbacks of DFT when compared with the Feature based detections SIFT/SURF?

    ReplyDelete
  4. Trung tâm dạy học kế toán tổng hợp Tại cầu giấy
    Trung tâm dạy học kế toán tổng hợp Tại từ liêm
    Trung tâm dạy học kế toán tổng hợp Tại thanh xuân
    Trung tâm dạy học kế toán tổng hợp Tại hà đông
    Trung tâm dạy học kế toán tổng hợp Tại long biên
    Trung tâm dạy học kế toán tổng hợp Tại nguyễn chính thanh đống đa
    Trung tâm dạy học kế toán tổng hợp Tại minh khai hai bà trưng
    Trung tâm dạy học kế toán tổng hợp Tại bắc ninh
    Trung tâm dạy học kế toán tổng hợp Tại hải phòng
    Trung tâm dạy học kế toán tổng hợp Tại tphcm
    Trung tâm dạy học kế toán tổng hợp Tại quận 3
    Trung tâm dạy học kế toán tổng hợp Tại thủ đức
    Trung tâm dạy học kế toán tổng hợp Tại đà nẵng
    Trung tâm dạy học kế toán tổng hợp Tại biên hòa
    Trung tâm dạy học kế toán tổng hợp Tại đồng nai
    Trung tâm dạy học kế toán tổng hợp Tại nam định
    Trung tâm dạy học kế toán tổng hợp Tại thái bình
    Trung tâm dạy học kế toán tổng hợp Tại bắc giang
    Trung tâm dạy học kế toán tổng hợp Tại vĩnh phúc
    Trung tâm dạy học kế toán tổng hợp Tại thái nguyên
    Trung tâm dạy học kế toán tổng hợp Tại quảng ninh
    Trung tâm dạy học kế toán tổng hợp Tại hải dương
    Trung tâm dạy học kế toán tổng hợp Tại hưng yên
    Trung tâm dạy học kế toán tổng hợp Tại hà nam
    Trung tâm dạy học kế toán tổng hợp Tại ninh bình
    Trung tâm dạy học kế toán tổng hợp Tại nghệ an
    Trung tâm dạy học kế toán tổng hợp Tại vũng tàu

    ReplyDelete
  5. Thanks for your helpful post on Fourier Transform. Retail store owners who require an efficient POS software for day to day sales and inventory management operations may checkout TradeMeters retail point of sale software which is suitable for many different types of stores. This retail POS Software can be used as a jewelry store point of sale software as well.

    ReplyDelete
  6. Thanks for this informative article, I hope you will get most positive response specially for this post. . . .
    โกเด้นสล็อต

    ReplyDelete
  7. Post more than Great , Kindly Thanks


    spot7.org

    ReplyDelete