From aad9b3219ce18f9ea1c682bc2b22fcce8d6737d2 Mon Sep 17 00:00:00 2001 From: Kirill Kornyakov Date: Wed, 8 Jun 2011 06:45:21 +0000 Subject: [PATCH] Notification messages about bad parameters in command line were added in CommandLineParser. Update sample, using parser --- modules/core/include/opencv2/core/core.hpp | 1 - modules/core/src/cmdparser.cpp | 86 +++++++++++++++++----- samples/cpp/brief_match_test.cpp | 40 ++++++---- 3 files changed, 92 insertions(+), 35 deletions(-) diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index bc2a324d7..8eeb069f4 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -4233,7 +4233,6 @@ class CV_EXPORTS CommandLineParser template _Tp analyzeValue(const std::string& str); }; - template<> CV_EXPORTS std::string CommandLineParser::analyzeValue(const std::string& str); diff --git a/modules/core/src/cmdparser.cpp b/modules/core/src/cmdparser.cpp index 4edfe41b9..99ab413ec 100644 --- a/modules/core/src/cmdparser.cpp +++ b/modules/core/src/cmdparser.cpp @@ -3,6 +3,31 @@ using namespace std; using namespace cv; +void helpParser() +{ + printf("\nThe CommandLineParser class is designed for command line arguments parsing\n" + "Supported syntax: \n" + " --key1=arg1 or --key3 \n" + "Usage: \n" + " Imagine that the input parameters are next:\n" + " -k=10 --key --db=-10.11 -key1 argument --inputFile=lena.jpg\n" + "parser.get(\"k\")\n" + "parser.get(\"db\", 99.99)\n" + " It also works with 'int', 'unsigned int', 'float' and 'string' types\n" + "parser.get(\"0\")\n" + "parser.get(\"1\")\n" + "parser.get(\"2\")\n\n" + ); +} vector split_string(const string& str, const string& delimiters) { @@ -24,7 +49,6 @@ CommandLineParser::CommandLineParser(int argc, const char* argv[]) std::string cur_name; std::string buffer; std::stringstream str_buff(std::stringstream::in | std::stringstream::out); - std::string str_index; std::map::iterator it; int find_symbol; int index = 0; @@ -48,41 +72,65 @@ CommandLineParser::CommandLineParser(int argc, const char* argv[]) buffer.erase(0, find_symbol + 1); if (data.find(cur_name) != data.end()) { - string str_exception="dublicating parameters for name='" + cur_name + "'"; - CV_Error(CV_StsParseError, str_exception); + printf("CommandLineParser constructor found dublicating parameters for name=%s\n" + , cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without dublicates\n"); + helpParser(); + data.clear(); + break; } else data[cur_name] = buffer; } else if (cur_name.find('=') == 0) { - string str_exception="This key is wrong. The key mustn't have '=' like increment' '" + cur_name + "'"; - CV_Error(CV_StsParseError, str_exception); + printf("The next key is wrong: key= %s\n", cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without any mistakes\n"); + helpParser(); + data.clear(); + break; } else if(((int)cur_name.find('-') == -1) && ((int)cur_name.find('=') != -1)) { - string str_exception="This key must be defined with '--' or '-' increment'" + cur_name + "'"; - CV_Error(CV_StsParseError, str_exception); + printf("The next key must be defined with '--' or '-' increment: key= %s\n", cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without any mistakes\n"); + helpParser(); + data.clear(); + break; } else if (cur_name.find('=') == (cur_name.length() - 1)) { - string str_exception="This key must have argument after '=''" + cur_name + "'"; - CV_Error(CV_StsParseError, str_exception); + printf("The next key must have argument after '=': key= %s\n", cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without any mistakes\n"); + helpParser(); + data.clear(); + break; } else { - str_buff<< index; - str_index = str_buff.str(); - str_buff.seekp(0); - for(it = data.begin(); it != data.end(); it++) + str_buff << index; + while (cur_name.find('-') == 0) + cur_name.erase(0,1); + + for(it = data.begin(); it != data.end(); it++) + { + if (it->second == cur_name) { - if (it->second == cur_name) - { - string str_exception="dublicating parameters for name='" + cur_name + "'"; - CV_Error(CV_StsParseError, str_exception); - } + printf("CommandLineParser constructor found dublicating parameters for name=%s\n" + , cur_name.c_str()); + printf("Constructor will not continue its work since this moment.\n" + "Please enter parameters without dublicates\n"); + helpParser(); + data.clear(); + break; } - data[str_index.c_str()] = cur_name; + } + data[str_buff.str()] = cur_name; + str_buff.seekp(0); index++; } diff --git a/samples/cpp/brief_match_test.cpp b/samples/cpp/brief_match_test.cpp index 5c491dac2..0650e30d3 100644 --- a/samples/cpp/brief_match_test.cpp +++ b/samples/cpp/brief_match_test.cpp @@ -4,6 +4,7 @@ * Created on: Oct 17, 2010 * Author: ethan */ +#include "opencv2/core/core.hpp" #include "opencv2/calib3d/calib3d.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/imgproc/imgproc.hpp" @@ -11,6 +12,7 @@ #include #include +using namespace std; using namespace cv; using std::cout; @@ -18,13 +20,15 @@ using std::cerr; using std::endl; using std::vector; -void help(char **av) -{ - cerr << "usage: " << av[0] << " im1.jpg im2.jpg" - << "\n" - << "This program shows how to use BRIEF descriptor to match points in features2d\n" - << "It takes in two images, finds keypoints and matches them displaying matches and final homography warped results\n" - << endl; +void help() +{ + printf("\nThis program shows how to use BRIEF descriptor to match points in features2d\n" + "It takes in two images, finds keypoints and matches them displaying matches and final homography warped results\n" + "Usage: \n" + " ./brief_match_test [--first_file]= \n" + " [--second_file]= \n" + "Example: \n" + "./brief_match_test --first_file=left01.jpg --second_file=left02.jpg \n"); } //Copy (x,y) location of descriptor matches found from KeyPoint data structures into Point2f vectors @@ -55,16 +59,22 @@ double match(const vector& /*kpts_train*/, const vector& /*k -int main(int ac, char ** av) +int main(int ac, const char ** av) { - if (ac != 3) - { - help(av); - return 1; - } + help(); + + CommandLineParser parser(ac, av); + string im1_name, im2_name; - im1_name = av[1]; - im2_name = av[2]; + im1_name = parser.get("first_file", "left01.jpg"); + im2_name = parser.get("second_file", "left02.jpg"); + + if (im1_name.empty() || im2_name.empty()) + { + help(); + printf("\n You have to indicate two files first_file and second_file \n"); + return -1; + } Mat im1 = imread(im1_name, CV_LOAD_IMAGE_GRAYSCALE); Mat im2 = imread(im2_name, CV_LOAD_IMAGE_GRAYSCALE);