1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
9 // This file originates from the openFABMAP project:
10 // [http://code.google.com/p/openfabmap/]
12 // For published work which uses all or part of OpenFABMAP, please cite:
13 // [http://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6224843]
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]
22 // Copyright (C) 2012 Arren Glover [aj.glover@qut.edu.au] and
23 // Will Maddern [w.maddern@qut.edu.au], all rights reserved.
26 // Redistribution and use in source and binary forms, with or without modification,
27 // are permitted provided that the following conditions are met:
29 // * Redistribution's of source code must retain the above copyright notice,
30 // this list of conditions and the following disclaimer.
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.
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.
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.
54 #include "opencv2/contrib.hpp"
55 #include "opencv2/highgui.hpp"
56 #include "opencv2/nonfree.hpp"
61 int main(int argc, char * argv[]) {
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.
69 A new vocabulary can be generated using the supplied BOWMSCtrainer (or other
70 clustering method such as K-means
72 New training data can be generated by extracting bag-of-words using the
73 openCV BOWImgDescriptorExtractor class.
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
81 cout << "This sample program demonstrates the FAB-MAP image matching "
82 "algorithm" << endl << endl;
87 } else if (argc == 2) {
88 dataDir = string(argv[1]);
92 cout << "Usage: fabmap_sample <sample data directory>" <<
100 cout << "Loading Vocabulary: " <<
101 dataDir + string("vocab_small.yml") << endl << endl;
102 fs.open(dataDir + string("vocab_small.yml"), FileStorage::READ);
104 fs["Vocabulary"] >> vocab;
106 cerr << "Vocabulary not found" << endl;
111 //load/generate training data
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);
117 fs["BOWImageDescs"] >> trainData;
118 if (trainData.empty()) {
119 cerr << "Training Data not found" << endl;
124 //create Chow-liu tree
125 cout << "Making Chow-Liu Tree from training data" << endl <<
127 of2::ChowLiuTree treeBuilder;
128 treeBuilder.add(trainData);
129 Mat tree = treeBuilder.make();
132 cout << "Extracting Test Data from images" << 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");
142 BOWImgDescriptorExtractor bide(extractor, matcher);
143 bide.setVocabulary(vocab);
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"));
160 vector<KeyPoint> kpts;
162 for(size_t i = 0; i < imageNames.size(); i++) {
163 cout << dataDir + imageNames[i] << endl;
164 frame = imread(dataDir + imageNames[i]);
166 cerr << "Test images not found" << endl;
170 detector->detect(frame, kpts);
172 bide.compute(frame, kpts, bow);
174 testData.push_back(bow);
176 drawKeypoints(frame, kpts, frame);
177 imshow(imageNames[i], frame);
182 cout << "Running FAB-MAP algorithm" << endl <<
184 Ptr<of2::FabMap> fabmap;
186 fabmap.reset(new of2::FabMap2(tree, 0.39, 0, of2::FabMap::SAMPLED |
187 of2::FabMap::CHOW_LIU));
188 fabmap->addTraining(trainData);
190 vector<of2::IMatch> matches;
191 fabmap->compare(testData, matches, true);
194 Mat result_small = Mat::zeros(10, 10, CV_8UC1);
195 vector<of2::IMatch>::iterator l;
197 for(l = matches.begin(); l != matches.end(); l++) {
199 result_small.at<char>(l->queryIdx, l->queryIdx) =
200 (char)(l->match*255);
203 result_small.at<char>(l->queryIdx, l->imgIdx) =
204 (char)(l->match*255);
208 Mat result_large(100, 100, CV_8UC1);
209 resize(result_small, result_large, Size(500, 500), 0, 0, INTER_NEAREST);
211 cout << endl << "Press any key to exit" << endl;
212 imshow("Confusion Matrix", result_large);