OpenCV  3.1.0 Open Source Computer Vision
Discrete Fourier Transform

## Goal

We'll seek answers for the following questions:

## Source code

You can download this from here or find it in the samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp of the OpenCV source code library.

Here's a sample usage of cv::dft() :

1 #include "opencv2/core/core.hpp"
3 #include "opencv2/imgcodecs.hpp"
5
6 #include <iostream>
7
8 using namespace cv;
9 using namespace std;
10
11 static void help(char* progName)
12 {
13  cout << endl
14  << "This program demonstrated the use of the discrete Fourier transform (DFT). " << endl
15  << "The dft of an image is taken and it's power spectrum is displayed." << endl
16  << "Usage:" << endl
17  << progName << " [image_name -- default ../data/lena.jpg] " << endl << endl;
18 }
19
20 int main(int argc, char ** argv)
21 {
22  help(argv[0]);
23
24  const char* filename = argc >=2 ? argv[1] : "../data/lena.jpg";
25
27  if( I.empty())
28  return -1;
29
30  Mat padded; //expand input image to optimal size
31  int m = getOptimalDFTSize( I.rows );
32  int n = getOptimalDFTSize( I.cols ); // on the border add zero values
33  copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
34
36  Mat complexI;
37  merge(planes, 2, complexI); // Add to the expanded another plane with zeros
38
39  dft(complexI, complexI); // this way the result may fit in the source matrix
40
41  // compute the magnitude and switch to logarithmic scale
42  // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
43  split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
44  magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
45  Mat magI = planes[0];
46
47  magI += Scalar::all(1); // switch to logarithmic scale
48  log(magI, magI);
49
50  // crop the spectrum, if it has an odd number of rows or columns
51  magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
52
53  // rearrange the quadrants of Fourier image so that the origin is at the image center
54  int cx = magI.cols/2;
55  int cy = magI.rows/2;
56
57  Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
58  Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-Right
59  Mat q2(magI, Rect(0, cy, cx, cy)); // Bottom-Left
60  Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right
61
62  Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
63  q0.copyTo(tmp);
64  q3.copyTo(q0);
65  tmp.copyTo(q3);
66
67  q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
68  q2.copyTo(q1);
69  tmp.copyTo(q2);
70
71  normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
72  // viewable image form (float between values 0 and 1).
73
74  imshow("Input Image" , I ); // Show the result
75  imshow("spectrum magnitude", magI);
76  waitKey();
77
78  return 0;
79 }
bool empty() const
Returns true if the array has no elements.
void split(const Mat &src, Mat *mvbegin)
Divides a multi-channel array into several single-channel arrays.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: mat.hpp:1885
int getOptimalDFTSize(int vecsize)
Returns the optimal DFT size for a given vector size.
Loads an image from a file.
STL namespace.
void copyTo(OutputArray m) const
Copies the matrix to another one.
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
#define CV_32F
Definition: cvdef.h:369
void magnitude(InputArray x, InputArray y, OutputArray magnitude)
Calculates the magnitude of 2D vectors.
Definition: affine.hpp:51
void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar &value=Scalar())
Forms a border around an image.
void dft(InputArray src, OutputArray dst, int flags=0, int nonzeroRows=0)
Performs a forward or inverse Discrete Fourier transform of a 1D or 2D floating-point array...
int cols
Definition: mat.hpp:1885
Rect2i Rect
Definition: types.hpp:409
MatSize size
Definition: mat.hpp:1904
static Vec< _Tp, cn > normalize(const Vec< _Tp, cn > &v)
If set, always convert image to the single channel grayscale image.
Definition: imgcodecs.hpp:66
void log(InputArray src, OutputArray dst)
Calculates the natural logarithm of every array element.
void merge(const Mat *mv, size_t count, OutputArray dst)
Creates one multi-channel array out of several single-channel ones.
iiiiii|abcdefgh|iiiiiii with some specified i
Definition: base.hpp:252
flag
Definition: base.hpp:194
static Scalar_< double > all(doublev0)
returns a scalar with all elements set to v0
n-dimensional dense array class
Definition: mat.hpp:740
int waitKey(int delay=0)
Waits for a pressed key.

## Explanation

The Fourier Transform will decompose an image into its sinus and cosines components. In other words, it will transform an image from its spatial domain to its frequency domain. The idea is that any function may be approximated exactly with the sum of infinite sinus and cosines functions. The Fourier Transform is a way how to do this. 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. Displaying this is possible either via a real image and a complex image or via a magnitude and a phase image. However, throughout the image processing algorithms only the magnitude image is interesting as this contains all the information we need about the images geometric structure. Nevertheless, if you intend to make some modifications of the image in these forms and then you need to retransform it you'll need to preserve both of these.

In this sample I'll show how to calculate and show the magnitude image of a Fourier Transform. In case of digital images are discrete. This means they may take up a value from a given domain value. For example in a basic gray scale image values usually are between zero and 255. Therefore the Fourier Transform too needs to be of a discrete type resulting in a Discrete Fourier Transform (DFT). You'll want to use this whenever you need to determine the structure of an image from a geometrical point of view. Here are the steps to follow (in case of a gray scale input image I):

1. Expand the image to an optimal size. The performance of a DFT is dependent of the image size. It tends to be the fastest for image sizes that are multiple of the numbers two, three and five. Therefore, to achieve maximal performance it is generally a good idea to pad border values to the image to get a size with such traits. The cv::getOptimalDFTSize() returns this optimal size and we can use the cv::copyMakeBorder() function to expand the borders of an image:
Mat padded; //expand input image to optimal size
int m = getOptimalDFTSize( I.rows );
int n = getOptimalDFTSize( I.cols ); // on the border add zero pixels
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
The appended pixels are initialized with zero.
2. Make place for both the complex and the real values. The result of a Fourier Transform is complex. This implies that for each image value the result is two image values (one per component). Moreover, the frequency domains range is much larger than its spatial counterpart. Therefore, we store these usually at least in a float format. Therefore we'll convert our input image to this type and expand it with another channel to hold the complex values:
Mat complexI;
merge(planes, 2, complexI); // Add to the expanded another plane with zeros
3. Make the Discrete Fourier Transform. It's possible an in-place calculation (same input as output):
dft(complexI, complexI); // this way the result may fit in the source matrix
4. Transform the real and complex values to magnitude. A complex number has a real (Re) and a complex (imaginary - Im) part. The results of a DFT are complex numbers. The magnitude of a DFT is:

$M = \sqrt[2]{ {Re(DFT(I))}^2 + {Im(DFT(I))}^2}$

Translated to OpenCV code:

split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat magI = planes[0];
5. Switch to a logarithmic scale. It turns out that the dynamic range of the Fourier coefficients is too large to be displayed on the screen. We have some small and some high changing values that we can't observe like this. Therefore the high values will all turn out as white points, while the small ones as black. To use the gray scale values to for visualization we can transform our linear scale to a logarithmic one:

$M_1 = \log{(1 + M)}$

Translated to OpenCV code:

magI += Scalar::all(1); // switch to logarithmic scale
log(magI, magI);
6. Crop and rearrange. Remember, that at the first step, we expanded the image? Well, it's time to throw away the newly introduced values. For visualization purposes we may also rearrange the quadrants of the result, so that the origin (zero, zero) corresponds with the image center.
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
Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
q2.copyTo(q1);
tmp.copyTo(q2);
7. Normalize. This is done again for visualization purposes. We now have the magnitudes, however this are still out of our image display range of zero to one. We normalize our values to this range using the cv::normalize() function.
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
// viewable image form (float between values 0 and 1).

## Result

An application idea would be to determine the geometrical orientation present in the image. For example, let us find out if a text is horizontal or not? Looking at some text you'll notice that the text lines sort of form also horizontal lines and the letters form sort of vertical lines. These two main components of a text snippet may be also seen in case of the Fourier transform. Let us use this horizontal and this rotated image about a text.

In case of the horizontal text:

In case of a rotated text:

You can see that the most influential components of the frequency domain (brightest dots on the magnitude image) follow the geometric rotation of objects on the image. From this we may calculate the offset and perform an image rotation to correct eventual miss alignments.