Commit 6d3e9251 authored by itsyplen's avatar itsyplen

new version of command line parser

parent 10444f4e
...@@ -4231,78 +4231,108 @@ protected: ...@@ -4231,78 +4231,108 @@ protected:
#endif #endif
/*! /*!
Command Line Parser "\nThe CommandLineParser class is designed for command line arguments parsing\n"
"Keys map: \n"
The class is used for reading command arguments. "Before you start to work with CommandLineParser you have to create a map for keys.\n"
Supports the following syntax: " It will look like this\n"
//-k=10 --key --db=-10.11 -key1 argument --inputFile=lena.jpg " const char* keys =\n"
CommandLineParser parser(argc, argv); " {\n"
int k = parser.get<int>("k", -1); //these methods also work " { s| string| 123asd |string parameter}\n"
double db = parser.get<double>("db"); //with <float> and <unsigned int> type " { d| digit | 100 |digit parameter }\n"
bool key = parser.get<bool>("key"); <The method return 'true', if 'key' was defined in command line " { c|noCamera|false |without camera }\n"
" and it will return 'false' otherwise.> " { 1| |some text|help }\n"
bool key1 = parser.get<bool>("key1"); The method return 'true', if 'key' was defined in command line " { 2| |333 |another help }\n"
" and it will return 'false' otherwise.> " };\n"
string argument = parser.get<string>("0"); <If you need to take argument. It's the first parameter without '-' or '--' increment "Usage syntax: \n"
and without value. It has index 0. The second parameter of this type will have index 1> " \"{\" - start of parameter string.\n"
It also works with 'int', 'unsigned int', 'double' and 'float' types. " \"}\" - end of parameter string\n"
string inputFile = parser.get<string>("inputFile"); " \"|\" - separator between short name, full name, default value and help\n"
"Supported syntax: \n"
" --key1=arg1 <If a key with '--' must has an argument\n"
" you have to assign it through '=' sign.> \n"
"<If the key with '--' doesn't have any argument, it means that it is a bool key>\n"
" -key2=arg2 <If a key with '-' must has an argument \n"
" you have to assign it through '=' sign.> \n"
"If the key with '-' doesn't have any argument, it means that it is a bool key\n"
" key3 <This key can't has any parameter> \n"
"Usage: \n"
" Imagine that the input parameters are next:\n"
" -s=string_value --digit=250 --noCamera lena.jpg 10000\n"
" CommandLineParser parser(argc, argv, keys) - create a parser object\n"
" parser.get<string>(\"s\" or \"string\") will return you first parameter value\n"
" parser.get<string>(\"s\", false or \"string\", false) will return you first parameter value\n"
" without spaces in end and begin\n"
" parser.get<int>(\"d\" or \"digit\") will return you second parameter value.\n"
" It also works with 'unsigned int', 'double', and 'float' types>\n"
" parser.get<bool>(\"c\" or \"noCamera\") will return you true .\n"
" If you enter this key in commandline>\n"
" It return you false otherwise.\n"
" parser.get<string>(\"1\") will return you the first argument without parameter (lena.jpg) \n"
" parser.get<int>(\"2\") will return you the second argument without parameter (10000)\n"
" It also works with 'unsigned int', 'double', and 'float' types \n"
*/ */
class CV_EXPORTS CommandLineParser class CV_EXPORTS CommandLineParser
{ {
public: public:
//! the default constructor //! the default constructor
CommandLineParser(int argc, const char* argv[]); CommandLineParser(int argc, const char* argv[], const char* key_map);
//! get parameter, if parameter is not given get default parameter //! get parameter, you can choose: delete spaces in end and begin or not
template<typename _Tp> template<typename _Tp>
_Tp get(const std::string& name, const _Tp& default_value = _Tp()) _Tp get(const std::string& name, bool space_delete=true)
{
if (!has(name))
{ {
std::string str = getString(name); return _Tp();
if (!has(name))
return default_value;
return analyzeValue<_Tp>(str);
} }
std::string str = getString(name);
return analizeValue<_Tp>(str, space_delete);
}
//! print short name, full name, current value and help for all params
void printParams();
protected: protected:
std::map<std::string, std::string > data; std::map<std::string, std::vector<std::string> > data;
std::string getString(const std::string& name) const; std::string getString(const std::string& name);
bool has(const std::string& keys) const; bool has(const std::string& keys);
template<typename _Tp> template<typename _Tp>
static _Tp getData(const std::string& str) _Tp analizeValue(const std::string& str, bool space_delete=false);
{
_Tp res;
std::stringstream s1(str);
s1 >> res;
return res;
}
template<typename _Tp> template<typename _Tp>
_Tp fromStringNumber(const std::string& str);//the default conversion function for numbers static _Tp getData(const std::string& str)
{
_Tp res;
std::stringstream s1(str);
s1 >> res;
return res;
}
template<typename _Tp>
_Tp fromStringNumber(const std::string& str);//the default conversion function for numbers
template<typename _Tp>
_Tp analyzeValue(const std::string& str);
}; };
template<> CV_EXPORTS
bool CommandLineParser::get<bool>(const std::string& name, const bool& default_value);
template<> CV_EXPORTS template<> CV_EXPORTS
std::string CommandLineParser::analyzeValue<std::string>(const std::string& str); bool CommandLineParser::get<bool>(const std::string& name, bool space_delete);
template<> CV_EXPORTS
std::string CommandLineParser::analizeValue<std::string>(const std::string& str, bool space_delete);
template<> CV_EXPORTS template<> CV_EXPORTS
int CommandLineParser::analyzeValue<int>(const std::string& str); int CommandLineParser::analizeValue<int>(const std::string& str, bool space_delete);
template<> CV_EXPORTS template<> CV_EXPORTS
unsigned CommandLineParser::analyzeValue<unsigned int>(const std::string& str); unsigned CommandLineParser::analizeValue<unsigned int>(const std::string& str, bool space_delete);
template<> CV_EXPORTS template<> CV_EXPORTS
float CommandLineParser::analyzeValue<float>(const std::string& str); float CommandLineParser::analizeValue<float>(const std::string& str, bool space_delete);
template<> CV_EXPORTS template<> CV_EXPORTS
double CommandLineParser::analyzeValue<double>(const std::string& str); double CommandLineParser::analizeValue<double>(const std::string& str, bool space_delete);
} }
......
#include "precomp.hpp" #include "precomp.hpp"
#include "iostream"
using namespace std; using namespace std;
using namespace cv; using namespace cv;
...@@ -6,27 +7,44 @@ using namespace cv; ...@@ -6,27 +7,44 @@ using namespace cv;
void helpParser() void helpParser()
{ {
printf("\nThe CommandLineParser class is designed for command line arguments parsing\n" printf("\nThe CommandLineParser class is designed for command line arguments parsing\n"
"Keys map: \n"
"Before you start to work with CommandLineParser you have to create a map for keys.\n"
" It will look like this\n"
" const char* keys =\n"
" {\n"
" { s| string| 123asd |string parameter}\n"
" { d| digit | 100 |digit parameter }\n"
" { c|noCamera|false |without camera }\n"
" { 1| |some text|help }\n"
" { 2| |333 |another help }\n"
" };\n"
"Usage syntax: \n"
" \"{\" - start of parameter string.\n"
" \"}\" - end of parameter string\n"
" \"|\" - separator between short name, full name, default value and help\n"
"Supported syntax: \n" "Supported syntax: \n"
" --key1=arg1 or --key3 <The keys with '--' can have argument.\n" " --key1=arg1 <If a key with '--' must has an argument\n"
"If it has argument, you should assign it through '=' sign> \n" " you have to assign it through '=' sign.> \n"
" -key2=arg2 or -key2 <The keys witn '-' can have argument \n" "<If the key with '--' doesn't have any argument, it means that it is a bool key>\n"
"If it has argument, you should assign it through '=' sign> \n" " -key2=arg2 <If a key with '-' must has an argument \n"
" you have to assign it through '=' sign.> \n"
"If the key with '-' doesn't have any argument, it means that it is a bool key\n"
" key3 <This key can't has any parameter> \n" " key3 <This key can't has any parameter> \n"
"Usage: \n" "Usage: \n"
" Imagine that the input parameters are next:\n" " Imagine that the input parameters are next:\n"
" -k=10 --key --db=-10.11 -key1 argument --inputFile=lena.jpg\n" " -s=string_value --digit=250 --noCamera lena.jpg 10000\n"
"parser.get<int>(\"k\")<If you need to take k value.\n" " CommandLineParser parser(argc, argv, keys) - create a parser object\n"
" It also works with 'unsigned int', 'double', 'float' and 'string' types>\n" " parser.get<string>(\"s\" or \"string\") will return you first parameter value\n"
"parser.get<double>(\"db\", 99.99)<If you need to take db value.\n" " parser.get<string>(\"s\", false or \"string\", false) will return you first parameter value\n"
" If its value is empty, you will get default value 99.99>\n" " without spaces in end and begin\n"
" It also works with 'int', 'unsigned int', 'float' and 'string' types\n" " parser.get<int>(\"d\" or \"digit\") will return you second parameter value.\n"
"parser.get<bool>(\"key\")<The method return 'true', if 'key' was defined in command line\n" " It also works with 'unsigned int', 'double', and 'float' types>\n"
" and it will return 'false' otherwise.>\n" " parser.get<bool>(\"c\" or \"noCamera\") will return you true .\n"
"parser.get<bool>(\"key1\")<The method return 'true', if 'key' was defined in command line\n" " If you enter this key in commandline>\n"
" and it will return 'false' otherwise.>\n" " It return you false otherwise.\n"
"parser.get<stirng>(\"0\")<If you need to take argument. It's the first parameter without '-' or '--' increment \n" " parser.get<string>(\"1\") will return you the first argument without parameter (lena.jpg) \n"
" and without value. It has index 0. The second parameter of this type will have index 1>\n" " parser.get<int>(\"2\") will return you the second argument without parameter (10000)\n"
" It also works with 'int', 'unsigned int', 'double' and 'float' types\n\n" " It also works with 'unsigned int', 'double', and 'float' types \n"
); );
} }
...@@ -37,166 +55,179 @@ vector<string> split_string(const string& str, const string& delimiters) ...@@ -37,166 +55,179 @@ vector<string> split_string(const string& str, const string& delimiters)
string::size_type pos = str.find_first_of(delimiters, lastPos); string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) while (string::npos != pos || string::npos != lastPos)
{ {
res.push_back(str.substr(lastPos, pos - lastPos)); res.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiters, pos); lastPos = str.find_first_not_of(delimiters, pos);
if (str[pos + 1] == '|' && str[pos] == '|')
{
res.push_back("");
if(str[pos + 2] == '|')
res.push_back("");
}
if (str[pos + 1] == '\0')
res.push_back("");
pos = str.find_first_of(delimiters, lastPos); pos = str.find_first_of(delimiters, lastPos);
} }
return res; return res;
} }
CommandLineParser::CommandLineParser(int argc, const char* argv[]) CommandLineParser::CommandLineParser(int argc, const char* argv[], const char* keys)
{ {
std::string cur_name;
std::string buffer;
std::stringstream str_buff(std::stringstream::in | std::stringstream::out);
std::map<std::string, std::string >::iterator it;
int find_symbol;
int index = 0;
std::string keys_buffer;
std::string values_buffer;
std::string buffer;
std::string curName;
std::vector<string> keysVector;
std::vector<string> paramVector;
std::map<std::string, std::vector<std::string> >::iterator it;
size_t flagPosition;
int currentIndex = 1;
bool isFound = false;
bool withNoKey = false;
bool hasValueThroughEq = false;
for(int i = 1; i < argc; i++) keys_buffer = keys;
while (!keys_buffer.empty())
{ {
if(!argv[i]) flagPosition = keys_buffer.find_first_of('}');
flagPosition++;
buffer = keys_buffer.substr(0, flagPosition);
keys_buffer.erase(0, flagPosition);
flagPosition = buffer.find('{');
if (flagPosition != buffer.npos)
buffer.erase(flagPosition, (flagPosition + 1));
flagPosition = buffer.find('}');
if (flagPosition != buffer.npos)
buffer.erase(flagPosition);
paramVector = split_string(buffer, "|");
buffer = paramVector[0];
if (atoi(buffer.c_str()) == 0)
buffer = buffer + '|' + paramVector[1];
paramVector.erase(paramVector.begin(), paramVector.begin() + 2);
data[buffer] = paramVector;
}
buffer.clear();
keys_buffer.clear();
paramVector.clear();
for (int i = 1; i < argc; i++)
{
if (!argv[i])
break; break;
cur_name = argv[i]; curName = argv[i];
if((cur_name.find('-') == 0) && ((int)cur_name.find('=') != -1) && if (curName.find('-') == 0 && ((curName[1] < '0') || (curName[1] > '9')))
(cur_name.find('=') != (cur_name.length() - 1))) {
while (curName.find('-') == 0)
curName.erase(curName.begin(), (curName.begin() + 1));
}
else
withNoKey = true;
if (curName.find('=') != curName.npos)
{
hasValueThroughEq = true;
buffer = curName;
curName.erase(curName.find('='));
buffer.erase(0, (buffer.find('=') + 1));
}
for(it = data.begin(); it != data.end(); it++)
{ {
while (cur_name.find('-') == 0) keys_buffer = it->first;
cur_name.erase(0,1); keysVector = split_string(keys_buffer, "| ");
if (keysVector.size() == 1)
buffer = cur_name; keysVector.push_back("");
find_symbol = (int)cur_name.find('='); values_buffer = it->second[0];
cur_name.erase(find_symbol); if (((curName == keysVector[0]) || (curName == keysVector[1])) && hasValueThroughEq)
buffer.erase(0, find_symbol + 1);
if (data.find(cur_name) != data.end())
{ {
printf("CommandLineParser constructor found dublicating parameters for name=%s\n" it->second[0] = buffer;
, cur_name.c_str()); isFound = true;
printf("Constructor will not continue its work since this moment.\n"
"Please enter parameters without dublicates\n");
helpParser();
data.clear();
break; break;
} }
else
data[cur_name] = buffer; if (!hasValueThroughEq && (values_buffer.find('false') == values_buffer.npos) &&
} ((curName == keysVector[0]) || (curName == keysVector[1])))
else if (cur_name.find('=') == 0)
{ {
printf("The next key is wrong: key= %s\n", cur_name.c_str()); it->second[0] = argv[++i];
printf("Constructor will not continue its work since this moment.\n" isFound = true;
"Please enter parameters without any mistakes\n"); break;
helpParser(); }
data.clear();
break; if (!hasValueThroughEq && (values_buffer.find('false') != values_buffer.npos)
} && ((curName == keysVector[0]) || (curName == keysVector[1])))
else if(((int)cur_name.find('-') == -1) && ((int)cur_name.find('=') != -1))
{ {
printf("The next key must be defined with '--' or '-' increment: key= %s\n", cur_name.c_str()); it->second[0] = "true";
printf("Constructor will not continue its work since this moment.\n" isFound = true;
"Please enter parameters without any mistakes\n"); break;
helpParser(); }
data.clear();
break; if (withNoKey)
} {
else if (cur_name.find('=') == (cur_name.length() - 1)) std::string noKeyStr = it->first;
if(atoi(noKeyStr.c_str()) == currentIndex)
{ {
printf("The next key must have argument after '=': key= %s\n", cur_name.c_str()); it->second[0] = curName;
printf("Constructor will not continue its work since this moment.\n" currentIndex++;
"Please enter parameters without any mistakes\n"); isFound = true;
helpParser();
data.clear();
break; break;
} }
else if ((cur_name.find('-') == 0) && ((cur_name[1] < '0') || (cur_name[1] > '9')) ) }
{ }
while (cur_name.find('-') == 0)
cur_name.erase(0,1);
for(it = data.begin(); it != data.end(); it++)
{
if (it->first == cur_name)
{
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[cur_name] = "true";
}
else
{
str_buff << index;
for(it = data.begin(); it != data.end(); it++)
{
if (it->second == cur_name)
{
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_buff.str()] = cur_name;
str_buff.seekp(0);
index++;
}
withNoKey = false;
hasValueThroughEq = false;
if(!isFound)
printf("The current parameter is not defined: %s\n", curName.c_str());
isFound = false;
} }
} }
bool CommandLineParser::has(const std::string& keys) const bool CommandLineParser::has(const std::string& keys)
{ {
vector<string> names=split_string(keys, " |"); std::map<std::string, std::vector<std::string> >::iterator it;
for(size_t j=0; j < names.size(); j++) { std::vector<string> keysVector;
if (data.find(names[j])!=data.end()) for(it = data.begin(); it != data.end(); it++)
{
keysVector = split_string(it->first, "| ");
if (keysVector.size() == 1)
keysVector.push_back("");
if ((keys == keysVector[0]) || (keys == keysVector[1]))
return true; return true;
} }
return false; return false;
} }
std::string CommandLineParser::getString(const std::string& keys) const std::string CommandLineParser::getString(const std::string& keys)
{ {
vector<string> names=split_string(keys, " |"); std::map<std::string, std::vector<std::string> >::iterator it;
std::vector<string> valueVector;
int found_index=-1;
for(size_t j=0; j < names.size(); j++) {
const string& cur_name=names[j];
bool is_cur_found=has(cur_name);
if (is_cur_found && (found_index >= 0)) { for(it = data.begin(); it != data.end(); it++)
string str_exception="dublicating parameters for " {
"name='" + names[found_index] + "' and name='"+cur_name+"'"; valueVector = split_string(it->first, "| ");
CV_Error(CV_StsParseError, str_exception); if (valueVector.size() == 1)
} valueVector.push_back("");
if ((keys == valueVector[0]) || (keys == valueVector[1]))
if (is_cur_found) return it->second[0];
found_index=(int)j;
} }
return string();
if (found_index<0)
return string();
return data.find(names[found_index])->second;
} }
template<typename _Tp> template<typename _Tp>
_Tp CommandLineParser::fromStringNumber(const std::string& str) //the default conversion function for numbers _Tp CommandLineParser::fromStringNumber(const std::string& str)//the default conversion function for numbers
{ {
if (str.empty())
CV_Error(CV_StsParseError, "Empty string cannot be converted to a number");
const char* c_str=str.c_str(); const char* c_str=str.c_str();
if((!isdigit(c_str[0])) if ((!isdigit(c_str[0]))
&& &&
( (
(c_str[0]!='-') || (strlen(c_str) <= 1) || ( !isdigit(c_str[1]) ) (c_str[0]!='-') || (strlen(c_str) <= 1) || ( !isdigit(c_str[1]) )
...@@ -204,71 +235,77 @@ template<typename _Tp> ...@@ -204,71 +235,77 @@ template<typename _Tp>
) )
{ {
CV_Error(CV_StsParseError, "The string '"+ str +"' cannot be converted to a number"); printf("This string cannot be converted to a number. Zero will be returned %s\n ", str.c_str());
return _Tp();
} }
return getData<_Tp>(str); return getData<_Tp>(str);
} }
template<typename _Tp> void CommandLineParser::printParams()
static _Tp getData(const std::string& str) {
{ std::map<std::string, std::vector<std::string> >::iterator it;
_Tp res; std::vector<string> keysVector;
std::stringstream s1(str); for(it = data.begin(); it != data.end(); it++)
s1 >> res; {
return res; keysVector = split_string(it->first, "| ");
} if (keysVector.size() == 1)
keysVector.push_back("");
template<typename _Tp> printf("\t%s [%8s] (%12s - by default) - %s\n", keysVector[0].c_str(),
static _Tp fromStringNumber(const std::string& str)//the default conversion function for numbers keysVector[1].c_str(), it->second[0].c_str(), it->second[1].c_str());
{ }
}
if (str.empty())
CV_Error(CV_StsParseError, "Empty string cannot be converted to a number");
const char* c_str=str.c_str();
if( !isdigit(c_str[0]) &&
(c_str[0] != '-' || strlen(c_str) <= 1 || !isdigit(c_str[1]) ))
CV_Error(CV_StsParseError, "The string '"+ str +"' cannot be converted to a number");
return getData<_Tp>(str);
}
template<> template<>
bool CommandLineParser::get<bool>(const std::string& name, const bool& /*default_value*/) bool CommandLineParser::get<bool>(const std::string& name, bool space_delete)
{ {
if (!has(name)) std::string str_buf = getString(name);
if (space_delete)
{
while (str_buf.find_first_of(' ') == 0)
str_buf.erase(0, 1);
while (str_buf.find_last_of(' ') == (str_buf.length() - 1))
str_buf.erase(str_buf.end() - 1, str_buf.end());
}
if (str_buf == "false")
return false; return false;
return true; return true;
} }
template<> template<>
std::string CommandLineParser::analyzeValue<std::string>(const std::string& str) std::string CommandLineParser::analizeValue<std::string>(const std::string& str, bool space_delete)
{ {
if (space_delete)
{
std::string str_buf = str;
while (str_buf.find_first_of(' ') == 0)
str_buf.erase(0, 1);
while (str_buf.find_last_of('-') == (str.length() - 1))
str_buf.erase(str_buf.end() - 1, str_buf.end());
return str_buf;
}
return str; return str;
} }
template<> template<>
int CommandLineParser::analyzeValue<int>(const std::string& str) int CommandLineParser::analizeValue<int>(const std::string& str, bool space_delete)
{ {
return fromStringNumber<int>(str); return fromStringNumber<int>(str);
} }
template<> template<>
unsigned int CommandLineParser::analyzeValue<unsigned int>(const std::string& str) unsigned int CommandLineParser::analizeValue<unsigned int>(const std::string& str, bool space_delete)
{ {
return fromStringNumber<unsigned int>(str); return fromStringNumber<unsigned int>(str);
} }
template<> template<>
float CommandLineParser::analyzeValue<float>(const std::string& str) float CommandLineParser::analizeValue<float>(const std::string& str, bool space_delete)
{ {
return fromStringNumber<float>(str); return fromStringNumber<float>(str);
} }
template<> template<>
double CommandLineParser::analyzeValue<double>(const std::string& str) double CommandLineParser::analizeValue<double>(const std::string& str, bool space_delete)
{ {
return fromStringNumber<double>(str); return fromStringNumber<double>(str);
} }
...@@ -20,25 +20,29 @@ ...@@ -20,25 +20,29 @@
Or: http://oreilly.com/catalog/9780596516130/ Or: http://oreilly.com/catalog/9780596516130/
ISBN-10: 0596516134 or: ISBN-13: 978-0596516130 ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
************************************************** */ ************************************************** */
#include "opencv2/core/core.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "opencv2/video/background_segm.hpp" using namespace std;
#include "opencv2/imgproc/imgproc_c.h" using namespace cv;
#include "opencv2/highgui/highgui.hpp"
//VARIABLES for CODEBOOK METHOD: //VARIABLES for CODEBOOK METHOD:
CvBGCodeBookModel* model = 0; CvBGCodeBookModel* model = 0;
const int NCHANNELS = 3; const int NCHANNELS = 3;
bool ch[NCHANNELS]={true,true,true}; // This sets what channels should be adjusted for background bounds bool ch[NCHANNELS]={true,true,true}; // This sets what channels should be adjusted for background bounds
void help(void) void help()
{ {
printf("\nLearn background and find foreground using simple average and average difference learning method:\n" printf("\nLearn background and find foreground using simple average and average difference learning method:\n"
"Originally from the book: Learning OpenCV by O'Reilly press\n" "Originally from the book: Learning OpenCV by O'Reilly press\n"
"\nUSAGE:\nbgfg_codebook [--nframes=300] [movie filename, else from camera]\n" "\nUSAGE:\n"
" bgfg_codebook [--nframes(-nf)=300] [--movie_filename(-mf)=tree.avi] [--camera(-c), use camera or not]\n"
"***Keep the focus on the video windows, NOT the consol***\n\n" "***Keep the focus on the video windows, NOT the consol***\n\n"
"INTERACTIVE PARAMETERS:\n" "INTERACTIVE PARAMETERS:\n"
"\tESC,q,Q - quit the program\n" "\tESC,q,Q - quit the program\n"
...@@ -65,15 +69,25 @@ void help(void) ...@@ -65,15 +69,25 @@ void help(void)
//USAGE: ch9_background startFrameCollection# endFrameCollection# [movie filename, else from camera] //USAGE: ch9_background startFrameCollection# endFrameCollection# [movie filename, else from camera]
//If from AVI, then optionally add HighAvg, LowAvg, HighCB_Y LowCB_Y HighCB_U LowCB_U HighCB_V LowCB_V //If from AVI, then optionally add HighAvg, LowAvg, HighCB_Y LowCB_Y HighCB_U LowCB_U HighCB_V LowCB_V
// //
int main(int argc, char** argv) const char *keys =
{ {
const char* filename = 0; "{nf|nframes |300 |frames number}"
"{c |camera |false |use the camera or not}"
"{mf|movie_file|tree.avi |used movie video file}"
};
int main(int argc, const char** argv)
{
help();
CommandLineParser parser(argc, argv, keys);
int nframesToLearnBG = parser.get<int>("nf");
bool useCamera = parser.get<bool>("c");
string filename = parser.get<string>("mf");
IplImage* rawImage = 0, *yuvImage = 0; //yuvImage is for codebook method IplImage* rawImage = 0, *yuvImage = 0; //yuvImage is for codebook method
IplImage *ImaskCodeBook = 0,*ImaskCodeBookCC = 0; IplImage *ImaskCodeBook = 0,*ImaskCodeBookCC = 0;
CvCapture* capture = 0; CvCapture* capture = 0;
int c, n, nframes = 0; int c, n, nframes = 0;
int nframesToLearnBG = 300;
model = cvCreateBGCodeBookModel(); model = cvCreateBGCodeBookModel();
...@@ -87,30 +101,15 @@ int main(int argc, char** argv) ...@@ -87,30 +101,15 @@ int main(int argc, char** argv)
bool pause = false; bool pause = false;
bool singlestep = false; bool singlestep = false;
for( n = 1; n < argc; n++ ) if( useCamera )
{
static const char* nframesOpt = "--nframes=";
if( strncmp(argv[n], nframesOpt, strlen(nframesOpt))==0 )
{
if( sscanf(argv[n] + strlen(nframesOpt), "%d", &nframesToLearnBG) == 0 )
{
help();
return -1;
}
}
else
filename = argv[n];
}
if( !filename )
{ {
printf("Capture from camera\n"); printf("Capture from camera\n");
capture = cvCaptureFromCAM( 0 ); capture = cvCaptureFromCAM( 0 );
} }
else else
{ {
printf("Capture from file %s\n",filename); printf("Capture from file %s\n",filename.c_str());
capture = cvCreateFileCapture( filename ); capture = cvCreateFileCapture( filename.c_str() );
} }
if( !capture ) if( !capture )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment