140b28f906a8b5b39af2c5b01525556e298d9642
[platform/upstream/opencv.git] / samples / cpp / freak_demo.cpp
1 //  demo.cpp
2 //
3 //      Here is an example on how to use the descriptor presented in the following paper:
4 //      A. Alahi, R. Ortiz, and P. Vandergheynst. FREAK: Fast Retina Keypoint. In IEEE Conference on Computer Vision and Pattern Recognition, 2012.
5 //  CVPR 2012 Open Source Award winner
6 //
7 //      Copyright (C) 2011-2012  Signal processing laboratory 2, EPFL,
8 //      Kirell Benzi (kirell.benzi@epfl.ch),
9 //      Raphael Ortiz (raphael.ortiz@a3.epfl.ch),
10 //      Alexandre Alahi (alexandre.alahi@epfl.ch)
11 //      and Pierre Vandergheynst (pierre.vandergheynst@epfl.ch)
12 //
13 //  Redistribution and use in source and binary forms, with or without modification,
14 //  are permitted provided that the following conditions are met:
15 //
16 //   * Redistribution's of source code must retain the above copyright notice,
17 //     this list of conditions and the following disclaimer.
18 //
19 //   * Redistribution's in binary form must reproduce the above copyright notice,
20 //     this list of conditions and the following disclaimer in the documentation
21 //     and/or other materials provided with the distribution.
22 //
23 //   * The name of the copyright holders may not be used to endorse or promote products
24 //     derived from this software without specific prior written permission.
25 //
26 //  This software is provided by the copyright holders and contributors "as is" and
27 //  any express or implied warranties, including, but not limited to, the implied
28 //  warranties of merchantability and fitness for a particular purpose are disclaimed.
29 //  In no event shall the Intel Corporation or contributors be liable for any direct,
30 //  indirect, incidental, special, exemplary, or consequential damages
31 //  (including, but not limited to, procurement of substitute goods or services;
32 //  loss of use, data, or profits; or business interruption) however caused
33 //  and on any theory of liability, whether in contract, strict liability,
34 //  or tort (including negligence or otherwise) arising in any way out of
35 //  the use of this software, even if advised of the possibility of such damage.
36
37 #include <iostream>
38 #include <string>
39 #include <vector>
40
41 #include <opencv2/core.hpp>
42 #include "opencv2/core/utility.hpp"
43 #include <opencv2/highgui.hpp>
44 #include <opencv2/features2d.hpp>
45 #include <opencv2/nonfree.hpp>
46
47 using namespace cv;
48
49 static void help( char** argv )
50 {
51     std::cout << "\nUsage: " << argv[0] << " [path/to/image1] [path/to/image2] \n"
52               << "This is an example on how to use the keypoint descriptor presented in the following paper: \n"
53               << "A. Alahi, R. Ortiz, and P. Vandergheynst. FREAK: Fast Retina Keypoint. \n"
54               << "In IEEE Conference on Computer Vision and Pattern Recognition, 2012. CVPR 2012 Open Source Award winner \n"
55               << std::endl;
56 }
57
58 int main( int argc, char** argv ) {
59     // check http://docs.opencv.org/doc/tutorials/features2d/table_of_content_features2d/table_of_content_features2d.html
60     // for OpenCV general detection/matching framework details
61
62     if( argc != 3 ) {
63         help(argv);
64         return -1;
65     }
66
67     // Load images
68     Mat imgA = imread(argv[1], IMREAD_GRAYSCALE );
69     if( !imgA.data ) {
70         std::cout<< " --(!) Error reading image " << argv[1] << std::endl;
71         return -1;
72     }
73
74     Mat imgB = imread(argv[2], IMREAD_GRAYSCALE );
75     if( !imgB.data ) {
76         std::cout << " --(!) Error reading image " << argv[2] << std::endl;
77         return -1;
78     }
79
80     std::vector<KeyPoint> keypointsA, keypointsB;
81     Mat descriptorsA, descriptorsB;
82     std::vector<DMatch> matches;
83
84     // DETECTION
85     // Any openCV detector such as
86     SurfFeatureDetector detector(2000,4);
87
88     // DESCRIPTOR
89     // Our proposed FREAK descriptor
90     // (rotation invariance, scale invariance, pattern radius corresponding to SMALLEST_KP_SIZE,
91     // number of octaves, optional vector containing the selected pairs)
92     // FREAK extractor(true, true, 22, 4, std::vector<int>());
93     FREAK extractor;
94
95     // MATCHER
96     // The standard Hamming distance can be used such as
97     // BFMatcher matcher(NORM_HAMMING);
98     // or the proposed cascade of hamming distance using SSSE3
99     BFMatcher matcher(extractor.defaultNorm());
100
101     // detect
102     double t = (double)getTickCount();
103     detector.detect( imgA, keypointsA );
104     detector.detect( imgB, keypointsB );
105     t = ((double)getTickCount() - t)/getTickFrequency();
106     std::cout << "detection time [s]: " << t/1.0 << std::endl;
107
108     // extract
109     t = (double)getTickCount();
110     extractor.compute( imgA, keypointsA, descriptorsA );
111     extractor.compute( imgB, keypointsB, descriptorsB );
112     t = ((double)getTickCount() - t)/getTickFrequency();
113     std::cout << "extraction time [s]: " << t << std::endl;
114
115     // match
116     t = (double)getTickCount();
117     matcher.match(descriptorsA, descriptorsB, matches);
118     t = ((double)getTickCount() - t)/getTickFrequency();
119     std::cout << "matching time [s]: " << t << std::endl;
120
121     // Draw matches
122     Mat imgMatch;
123     drawMatches(imgA, keypointsA, imgB, keypointsB, matches, imgMatch);
124
125     namedWindow("matches", WINDOW_KEEPRATIO);
126     imshow("matches", imgMatch);
127     waitKey(0);
128 }