D_COMMAND( std::ofstream debugFile( "Debug.txt" ); ); SET_DOUT( debugFile ); TCLAP::CmdLine cmd( "Canny edge detector.", ' ', ""); /*---------------------------------------------------------------------*/ //Define cmd arguments TCLAP::ValueArg<double> lowThresholdArg( "l", "low", "Low threshold value", false, 0.5, "Value relative to high threshold" ); cmd.add( lowThresholdArg ); 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 );
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; }
void VTOutput::usage(TCLAP::CmdLineInterface& c) { std::string s = ""; std::list<TCLAP::Arg*> args = c.getArgList(); //prints unlabeled arument list first for (TCLAP::ArgListIterator it = args.begin(); it != args.end(); it++) { if (typeid(**it)==typeid(TCLAP::UnlabeledValueArg<std::string>)) { TCLAP::UnlabeledValueArg<std::string> *i = (TCLAP::UnlabeledValueArg<std::string> *) (*it); s = i->getName(); } else if (typeid(**it)==typeid(TCLAP::UnlabeledMultiArg<std::string>)) { TCLAP::UnlabeledMultiArg<std::string> *i = (TCLAP::UnlabeledMultiArg<std::string> *) (*it); s = i->getName(); } } std::clog << c.getProgramName() << " v" << c.getVersion() << "\n\n"; std::clog << "description : " << c.getMessage() << "\n\n"; std::clog << "usage : vt " << c.getProgramName() << " [options] " << s << "\n\n"; //prints rest of arguments for (TCLAP::ArgListIterator it = args.begin(); it != args.end(); it++) { if (it==args.begin()) { std::clog << "options : "; } else { std::clog << " "; } if (typeid(**it)==typeid(TCLAP::ValueArg<std::string>) || typeid(**it)==typeid(TCLAP::ValueArg<uint32_t>) || typeid(**it)==typeid(TCLAP::ValueArg<int32_t>) || typeid(**it)==typeid(TCLAP::ValueArg<double>) || typeid(**it)==typeid(TCLAP::ValueArg<float>)) { TCLAP::ValueArg<std::string> *i = (TCLAP::ValueArg<std::string> *) (*it); std::clog << "-" << i->getFlag() << " " << i->getDescription() << "\n"; } else if (typeid(**it)==typeid(TCLAP::SwitchArg)) { TCLAP::SwitchArg *i = (TCLAP::SwitchArg *) (*it); std::clog << "-" << i->getFlag() << " " << i->getDescription() << "\n"; } else if (typeid(**it)==typeid(TCLAP::UnlabeledValueArg<std::string>)) { //ignored } else if (typeid(**it)==typeid(TCLAP::UnlabeledMultiArg<std::string>)) { //ignored } else { std::clog << "oops, argument type not handled\n"; } } std::clog << "\n"; }