DescriptorsMatcher::DescriptorsMatcher(const cv::FileStorage& fs) { generateDetector(fs); if(!feature_detector_) { std::cerr << "Feature Detector error" << std::endl; exit(-1002); } generateExtractor(fs); if(!descriptor_extractor_) { std::cerr << "Feature Extractor error" << std::endl; exit(-1003); } std::string extractorType; fs["FeatureOptions"]["ExtractorType"] >> extractorType; if (extractorType == "ORB" || extractorType == "BRISK" || extractorType == "FREAK") { matcher_ = new cv::FlannBasedMatcher(new cv::flann::LshIndexParams(5, 24, 2)); } else { matcher_ = cv::DescriptorMatcher::create("FlannBased"); } }
/* The openFabMapcli accepts a YML settings file, an example of which is provided. Modify options in the settings file for desired operation */ int main(int argc, char * argv[]) { //load the settings file std::string settfilename; if (argc == 1) { //assume settings in working directory settfilename = "settings.yml"; } else if (argc == 3) { if(std::string(argv[1]) != "-s") { //incorrect option return help(); } else { //settings provided as argument settfilename = std::string(argv[2]); } } else { //incorrect arguments return help(); } cv::FileStorage fs; fs.open(settfilename, cv::FileStorage::READ); if (!fs.isOpened()) { std::cerr << "Could not open settings file: " << settfilename << std::endl; return -1; } cv::Ptr<cv::FeatureDetector> detector = generateDetector(fs); if(!detector) { std::cerr << "Feature Detector error" << std::endl; return -1; } cv::Ptr<cv::DescriptorExtractor> extractor = generateExtractor(fs); if(!extractor) { std::cerr << "Feature Extractor error" << std::endl; return -1; } //std::string extractorType = fs["FeatureOptions"]["ExtractorType"]; //cv::Ptr<cv::DescriptorExtractor> extractor; //if(extractorType == "SIFT") { // extractor = new cv::SiftDescriptorExtractor(); //} else if(extractorType == "SURF") { // extractor = new cv::SurfDescriptorExtractor( // fs["FeatureOptions"]["SurfDetector"]["NumOctaves"], // fs["FeatureOptions"]["SurfDetector"]["NumOctaveLayers"], // (int)fs["FeatureOptions"]["SurfDetector"]["Extended"] > 0, // (int)fs["FeatureOptions"]["SurfDetector"]["Upright"] > 0); //} else { // std::cerr << "Could not create Descriptor Extractor. Please specify " // "extractor type in settings file" << std::endl; // return -1; //} //run desired function int result = 0; std::string function = fs["Function"]; if (function == "ShowFeatures") { result = showFeatures( fs["FilePaths"]["TrainPath"], detector); } else if (function == "GenerateVocabTrainData") { result = generateVocabTrainData(fs["FilePaths"]["TrainPath"], fs["FilePaths"]["TrainFeatDesc"], detector, extractor); } else if (function == "TrainVocabulary") { result = trainVocabulary(fs["FilePaths"]["Vocabulary"], fs["FilePaths"]["TrainFeatDesc"], fs["VocabTrainOptions"]["ClusterSize"]); } else if (function == "GenerateFABMAPTrainData") { result = generateBOWImageDescs(fs["FilePaths"]["TrainPath"], fs["FilePaths"]["TrainImagDesc"], fs["FilePaths"]["Vocabulary"], detector, extractor, fs["BOWOptions"]["MinWords"]); } else if (function == "TrainChowLiuTree") { result = trainChowLiuTree(fs["FilePaths"]["ChowLiuTree"], fs["FilePaths"]["TrainImagDesc"], fs["ChowLiuOptions"]["LowerInfoBound"]); } else if (function == "GenerateFABMAPTestData") { result = generateBOWImageDescs(fs["FilePaths"]["TestPath"], fs["FilePaths"]["TestImageDesc"], fs["FilePaths"]["Vocabulary"], detector, extractor, fs["BOWOptions"]["MinWords"]); } else if (function == "RunOpenFABMAP") { std::string placeAddOption = fs["FabMapPlaceAddition"]; bool addNewOnly = (placeAddOption == "NewMaximumOnly"); of2::FabMap *fabmap = generateFABMAPInstance(fs); if(fabmap) { result = openFABMAP(fs["FilePaths"]["TestImageDesc"], fabmap, fs["FilePaths"]["Vocabulary"], fs["FilePaths"]["FabMapResults"], addNewOnly); } } else { std::cerr << "Incorrect Function Type" << std::endl; result = -1; } std::cout << "openFABMAP done" << std::endl; std::cin.sync(); std::cin.ignore(); fs.release(); return result; }
OpenFABMap::OpenFABMap(cv::FileStorage &settings) : BoWMatcher< cv::of2::IMatch >() { // Create detector and extractor generateDetector(settings); if(!feature_detector_) { std::cerr << "Feature Detector error" << std::endl; exit(-1002); } generateExtractor(settings); if(!descriptor_extractor_) { std::cerr << "Feature Extractor error" << std::endl; exit(-1003); } cv::FileStorage fs; //load FabMap training data std::string fabmapTrainDataPath = settings["FilePaths"]["TrainImagDesc"], chowliutreePath = settings["FilePaths"]["ChowLiuTree"], vocabPath = settings["FilePaths"]["Vocabulary"]; std::cout << "Loading FabMap Training Data" << std::endl; fs.open(fabmapTrainDataPath, cv::FileStorage::READ); cv::Mat fabmapTrainData; fs["BOWImageDescs"] >> fabmapTrainData; if (fabmapTrainData.empty()) { std::cerr << fabmapTrainDataPath << ": FabMap Training Data not found" << std::endl; exit(-1004); } fs.release(); //load a chow-liu tree std::cout << "Loading Chow-Liu Tree" << std::endl; fs.open(chowliutreePath, cv::FileStorage::READ); fs["ChowLiuTree"] >> cl_tree_; if (cl_tree_.empty()) { std::cerr << chowliutreePath << ": Chow-Liu tree not found" << std::endl; exit(-1005); } fs.release(); //load the vocabulary std::cout << "Loading Vocabulary" << std::endl; fs.open(vocabPath, cv::FileStorage::READ); fs["Vocabulary"] >> vocabulary_; if (vocabulary_.empty()) { std::cerr << vocabPath << ": Vocabulary not found" << std::endl; exit(-1006); } fs.release(); //create options flags std::string newPlaceMethod = settings["openFabMapOptions"]["NewPlaceMethod"], bayesMethod = settings["openFabMapOptions"]["BayesMethod"]; int simpleMotionModel = settings["openFabMapOptions"]["SimpleMotion"], options = 0; if(newPlaceMethod == "Sampled") { options |= cv::of2::FabMap::SAMPLED; } else { options |= cv::of2::FabMap::MEAN_FIELD; } if(bayesMethod == "ChowLiu") { options |= cv::of2::FabMap::CHOW_LIU; } else { options |= cv::of2::FabMap::NAIVE_BAYES; } if(simpleMotionModel) { options |= cv::of2::FabMap::MOTION_MODEL; } //create an instance of the desired type of FabMap std::string fabMapVersion = settings["openFabMapOptions"]["FabMapVersion"]; if(fabMapVersion == "FABMAP1") { open_fab_map_ = new cv::of2::FabMap1(cl_tree_, settings["openFabMapOptions"]["PzGe"], settings["openFabMapOptions"]["PzGne"], options, settings["openFabMapOptions"]["NumSamples"]); } else if(fabMapVersion == "FABMAPLUT") { open_fab_map_ = new cv::of2::FabMapLUT(cl_tree_, settings["openFabMapOptions"]["PzGe"], settings["openFabMapOptions"]["PzGne"], options, settings["openFabMapOptions"]["NumSamples"], settings["openFabMapOptions"]["FabMapLUT"]["Precision"]); } else if(fabMapVersion == "FABMAPFBO") { open_fab_map_ = new cv::of2::FabMapFBO(cl_tree_, settings["openFabMapOptions"]["PzGe"], settings["openFabMapOptions"]["PzGne"], options, settings["openFabMapOptions"]["NumSamples"], settings["openFabMapOptions"]["FabMapFBO"]["RejectionThreshold"], settings["openFabMapOptions"]["FabMapFBO"]["PsGd"], settings["openFabMapOptions"]["FabMapFBO"]["BisectionStart"], settings["openFabMapOptions"]["FabMapFBO"]["BisectionIts"]); } else if(fabMapVersion == "FABMAP2") { open_fab_map_ = new cv::of2::FabMap2(cl_tree_, settings["openFabMapOptions"]["PzGe"], settings["openFabMapOptions"]["PzGne"], options); } else { std::cerr << "Could not identify openFABMAPVersion from settings file" << std::endl; exit(-1001); } //add the training data for use with the sampling method open_fab_map_->addTraining(fabmapTrainData); }