~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/samples/cpp/tutorial_code/core/discrete_fourier_transform/discrete_fourier_transform.cpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "opencv2/core/core.hpp"
 
2
#include "opencv2/imgproc/imgproc.hpp"
 
3
#include "opencv2/imgcodecs.hpp"
 
4
#include "opencv2/highgui/highgui.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
 
 
26
    Mat I = imread(filename, IMREAD_GRAYSCALE);
 
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
 
 
35
    Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
 
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
}