int main(int argc, char* argv[]) { try { // init comandline parser TCLAP::CmdLine cmd("Command description message", ' ', "1.0"); TCLAP::ValueArg<string> argOutputFile ("o", "output", "Output file", true, "", "string"); TCLAP::ValueArg<string> argFilter ("f", "filter", "Filter files according to pattern", false, ".SIFTComparison.feat.xml.gz","string"); TCLAP::ValueArg<int> argPRECLUSTER ("p", "precluster", "Number of descriptors to select in precluster-preprocessing (0 = no preclustering)",false,0 ,"int"); TCLAP::ValueArg<int> argBOWSIZE ("b", "bowsize", "Size of the BoW Dictionary", false, 1000, "int"); TCLAP::SwitchArg argBinaryInput ("i", "binary", "Read descriptors from binary archives", false); TCLAP::SwitchArg argVerbose ("v", "verbose", "Provide additional debugging output", false); TCLAP::UnlabeledValueArg<string> dirArg ( "directory", "Directory containing files with extracted features", true, "directory","string"); cmd.add( argOutputFile ); cmd.add( argFilter ); cmd.add( argPRECLUSTER ); cmd.add( argBOWSIZE ); cmd.add( argBinaryInput ); cmd.add( argVerbose ); cmd.add( dirArg ); // parse arguments cmd.parse( argc, argv ); // enable/disable verbose output VerboseOutput::verbose = argVerbose.getValue(); VerboseOutput::println(string("train"), "Create BoW of size %d", argBOWSIZE.getValue()); TermCriteria tcrit; tcrit.epsilon = 10; tcrit.maxCount = 10; tcrit.type = 1; BOWKMeansTrainer bowtrainer(argBOWSIZE.getValue(),tcrit,1,KMEANS_PP_CENTERS); VerboseOutput::println(string("train"), "Creating Visual Bag of Words"); string filter = argFilter.getValue(); if (argBinaryInput.getValue()) { filter = ".SIFTComparison.descriptors.dat"; } vector<string> files = getdir(dirArg.getValue(), filter); int i = 1; VerboseOutput::println(string("train"), "Reading features of directory '%s'", dirArg.getValue().c_str()); for (vector<string>::iterator filename = files.begin(); filename!=files.end(); ++filename) { VerboseOutput::println("train", "[%i of %i in directory '%s']", i++, files.size(), dirArg.getValue().c_str()); Mat descriptors; stringstream filePathss; filePathss << dirArg.getValue() << "/" << *filename; string filePath = filePathss.str(); VerboseOutput::println(string("train"), string("processing file '" + filePath + "'")); if (argBinaryInput.getValue()) { loadDescriptorsFromBinaryArchives(descriptors, filePath); } else { loadDescriptorsFromOpenCVFilestorage(descriptors, filePath); } if ((descriptors.rows == 0) || (descriptors.cols == 0)) { throw runtime_error("No Descriptors read for file: "); } VerboseOutput::println(string("train"), "%i descriptors loaded", descriptors.rows); if ((argPRECLUSTER.getValue() > 0) && (argPRECLUSTER.getValue() < descriptors.rows - 100)) { VerboseOutput::println(string("train"), string("pre-clustering")); Mat labels; Mat centers; kmeans(descriptors,argPRECLUSTER.getValue(),labels,tcrit,1, KMEANS_PP_CENTERS, centers); VerboseOutput::println(string("train"), "...add cluster centers of pre-clustering to bow"); bowtrainer.add(centers); } else { VerboseOutput::println(string("train"), "...add descriptors to bow"); bowtrainer.add(descriptors); } VerboseOutput::println(string("train"), "...current bow-size: %i", bowtrainer.descripotorsCount()); } // calculate vocabulary VerboseOutput::println(string("train"), string("Creating Vocabulary")); if (bowtrainer.descripotorsCount() <= argBOWSIZE.getValue()) { throw runtime_error("BoW size higher than number of loaded descriptors!"); } Mat vocab = bowtrainer.cluster(); // output results to file VerboseOutput::println(string("train"), string("Storing BoW to File")); writeVocabularyToFile(vocab, argOutputFile.getValue()); VerboseOutput::println(string("train"), string("Finished")); } catch (exception &e) // catch any exceptions { cout << "\n"; cout << "*** Training aborted!\n"; cout << " Reason: " << e.what() << "\n\n"; exit(1); } return 0; }
TCLAP::ValueArg<double> highThresholdArg( "t", "high", "High threshold value", false, 0.5, "" ); cmd.add( highThresholdArg ); /*---------------------------------------------------------------------*/ TCLAP::UnlabeledValueArg<std::string> inFilenameArg( "input", "Input image filename", true, "", "filename1" ); cmd.add( inFilenameArg ); TCLAP::UnlabeledValueArg<std::string> outFilenameArg( "output", "Output image filename", true, "", "filename2" ); cmd.add( outFilenameArg ); cmd.parse( argc, argv ); /***************************************************/ std::string inFilename = inFilenameArg.getValue(); std::string outFilename = outFilenameArg.getValue(); std::cout << "Loading file '" << inFilename << "' ..."; std::cout.flush(); M4D::Imaging::AImage::Ptr image = M4D::Imaging::ImageFactory::LoadDumpedImage( inFilename ); std::cout << "Done\n"; std::cout << "Initializing..."; std::cout.flush(); M4D::Imaging::PipelineContainer *container = NULL; FinishHook *hook = new FinishHook; M4D::Imaging::ConnectionInterfaceTyped< M4D::Imaging::AImage > *inConnection = NULL; M4D::Imaging::ConnectionInterfaceTyped< M4D::Imaging::AImage > *outConnection = NULL; M4D::Imaging::APipeFilter *filter = NULL; /*---------------------------------------------------------------------*/