cd06b55cb8f2f5ada4657ebe6b89703f648bcddf
[platform/upstream/opencv.git] / samples / cpp / fabmap_sample.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 // This file originates from the openFABMAP project:
10 // [http://code.google.com/p/openfabmap/]
11 //
12 // For published work which uses all or part of OpenFABMAP, please cite:
13 // [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
14 //
15 // Original Algorithm by Mark Cummins and Paul Newman:
16 // [http://ijr.sagepub.com/content/27/6/647.short]
17 // [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=5613942]
18 // [http://ijr.sagepub.com/content/30/9/1100.abstract]
19 //
20 //                           License Agreement
21 //
22 // Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
23 //                    Will Maddern [w.maddern@qut.edu.au], all rights reserved.
24 //
25 //
26 // Redistribution and use in source and binary forms, with or without modification,
27 // are permitted provided that the following conditions are met:
28 //
29 //   * Redistribution's of source code must retain the above copyright notice,
30 //     this list of conditions and the following disclaimer.
31 //
32 //   * Redistribution's in binary form must reproduce the above copyright notice,
33 //     this list of conditions and the following disclaimer in the documentation
34 //     and/or other materials provided with the distribution.
35 //
36 //   * The name of the copyright holders may not be used to endorse or promote products
37 //     derived from this software without specific prior written permission.
38 //
39 // This software is provided by the copyright holders and contributors "as is" and
40 // any express or implied warranties, including, but not limited to, the implied
41 // warranties of merchantability and fitness for a particular purpose are disclaimed.
42 // In no event shall the Intel Corporation or contributors be liable for any direct,
43 // indirect, incidental, special, exemplary, or consequential damages
44 // (including, but not limited to, procurement of substitute goods or services;
45 // loss of use, data, or profits; or business interruption) however caused
46 // and on any theory of liability, whether in contract, strict liability,
47 // or tort (including negligence or otherwise) arising in any way out of
48 // the use of this software, even if advised of the possibility of such damage.
49 //
50 //M*/
51
52 #include <iostream>
53
54 #include "opencv2/contrib.hpp"
55 #include "opencv2/highgui.hpp"
56 #include "opencv2/nonfree.hpp"
57
58 using namespace cv;
59 using namespace std;
60
61 int main(int argc, char * argv[]) {
62
63     /*
64
65     Note: the vocabulary and training data is specifically made for this openCV
66     example. It is not reccomended for use with other datasets as it is
67     intentionally small to reduce baggage in the openCV project.
68
69     A new vocabulary can be generated using the supplied BOWMSCtrainer (or other
70     clustering method such as K-means
71
72     New training data can be generated by extracting bag-of-words using the
73     openCV BOWImgDescriptorExtractor class.
74
75     vocabulary, chow-liu tree, training data, and test data can all be saved and
76     loaded using openCV's FileStorage class and it is not necessary to generate
77     data each time as done in this example
78
79     */
80
81     cout << "This sample program demonstrates the FAB-MAP image matching "
82         "algorithm" << endl << endl;
83
84     string dataDir;
85     if (argc == 1) {
86         dataDir = "fabmap/";
87     } else if (argc == 2) {
88         dataDir = string(argv[1]);
89         dataDir += "/";
90     } else {
91         //incorrect arguments
92         cout << "Usage: fabmap_sample <sample data directory>" <<
93             endl;
94         return -1;
95     }
96
97     FileStorage fs;
98
99     //load/generate vocab
100     cout << "Loading Vocabulary: " <<
101         dataDir + string("vocab_small.yml") << endl << endl;
102     fs.open(dataDir + string("vocab_small.yml"), FileStorage::READ);
103     Mat vocab;
104     fs["Vocabulary"] >> vocab;
105     if (vocab.empty()) {
106         cerr << "Vocabulary not found" << endl;
107         return -1;
108     }
109     fs.release();
110
111     //load/generate training data
112
113     cout << "Loading Training Data: " <<
114         dataDir + string("train_data_small.yml") << endl << endl;
115     fs.open(dataDir + string("train_data_small.yml"), FileStorage::READ);
116     Mat trainData;
117     fs["BOWImageDescs"] >> trainData;
118     if (trainData.empty()) {
119         cerr << "Training Data not found" << endl;
120         return -1;
121     }
122     fs.release();
123
124     //create Chow-liu tree
125     cout << "Making Chow-Liu Tree from training data" << endl <<
126         endl;
127     of2::ChowLiuTree treeBuilder;
128     treeBuilder.add(trainData);
129     Mat tree = treeBuilder.make();
130
131     //generate test data
132     cout << "Extracting Test Data from images" << endl <<
133         endl;
134     Ptr<FeatureDetector> detector(
135         new DynamicAdaptedFeatureDetector(
136             AdjusterAdapter::create("STAR"), 130, 150, 5));
137     Ptr<DescriptorExtractor> extractor(
138         new SurfDescriptorExtractor(1000, 4, 2, false, true));
139     Ptr<DescriptorMatcher> matcher =
140         DescriptorMatcher::create("FlannBased");
141
142     BOWImgDescriptorExtractor bide(extractor, matcher);
143     bide.setVocabulary(vocab);
144
145     vector<string> imageNames;
146     imageNames.push_back(string("stlucia_test_small0000.jpeg"));
147     imageNames.push_back(string("stlucia_test_small0001.jpeg"));
148     imageNames.push_back(string("stlucia_test_small0002.jpeg"));
149     imageNames.push_back(string("stlucia_test_small0003.jpeg"));
150     imageNames.push_back(string("stlucia_test_small0004.jpeg"));
151     imageNames.push_back(string("stlucia_test_small0005.jpeg"));
152     imageNames.push_back(string("stlucia_test_small0006.jpeg"));
153     imageNames.push_back(string("stlucia_test_small0007.jpeg"));
154     imageNames.push_back(string("stlucia_test_small0008.jpeg"));
155     imageNames.push_back(string("stlucia_test_small0009.jpeg"));
156
157     Mat testData;
158     Mat frame;
159     Mat bow;
160     vector<KeyPoint> kpts;
161
162     for(size_t i = 0; i < imageNames.size(); i++) {
163         cout << dataDir + imageNames[i] << endl;
164         frame = imread(dataDir + imageNames[i]);
165         if(frame.empty()) {
166             cerr << "Test images not found" << endl;
167             return -1;
168         }
169
170         detector->detect(frame, kpts);
171
172         bide.compute(frame, kpts, bow);
173
174         testData.push_back(bow);
175
176         drawKeypoints(frame, kpts, frame);
177         imshow(imageNames[i], frame);
178         waitKey(10);
179     }
180
181     //run fabmap
182     cout << "Running FAB-MAP algorithm" << endl <<
183         endl;
184     Ptr<of2::FabMap> fabmap;
185
186     fabmap.reset(new of2::FabMap2(tree, 0.39, 0, of2::FabMap::SAMPLED |
187         of2::FabMap::CHOW_LIU));
188     fabmap->addTraining(trainData);
189
190     vector<of2::IMatch> matches;
191     fabmap->compare(testData, matches, true);
192
193     //display output
194     Mat result_small = Mat::zeros(10, 10, CV_8UC1);
195     vector<of2::IMatch>::iterator l;
196
197     for(l = matches.begin(); l != matches.end(); l++) {
198             if(l->imgIdx < 0) {
199                 result_small.at<char>(l->queryIdx, l->queryIdx) =
200                     (char)(l->match*255);
201
202             } else {
203                 result_small.at<char>(l->queryIdx, l->imgIdx) =
204                     (char)(l->match*255);
205             }
206     }
207
208     Mat result_large(100, 100, CV_8UC1);
209     resize(result_small, result_large, Size(500, 500), 0, 0, INTER_NEAREST);
210
211     cout << endl << "Press any key to exit" << endl;
212     imshow("Confusion Matrix", result_large);
213     waitKey();
214
215     return 0;
216 }