我想为animal创建自定义HOG检测器。所以我想从OpenCV 3.1编译train_hog.cpp
示例。但是我对目录有疑问。
这是load_images
void load_images( const string & prefix, const string & filename, vector< Mat > & img_lst );
void load_images( const string & prefix, const string & filename, vector< Mat > & img_lst )
{
string line;
ifstream file;
file.open( (prefix+filename).c_str() );
if( !file.is_open() )
{
cerr << "Unable to open the list of images from " << filename << " filename." << endl;
exit( -1 );
}
bool end_of_parsing = false;
while( !end_of_parsing )
{
getline( file, line );
if( line.empty() ) // no more file to read
{
end_of_parsing = true;
break;
}
Mat img = imread( (prefix+line).c_str() ); // load the image
if( img.empty() ) // invalid image, just skip it.
continue;
#ifdef _DEBUG
imshow( "image", img );
waitKey( 10 );
#endif
img_lst.push_back( img.clone() );
}
}
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv, "{help h|| show help message}"
"{pd||pos_dir}{p||pos.lst}{nd||neg_dir}{n||neg.lst}");
if (parser.has("help"))
{
parser.printMessage();
exit(0);
}
vector< Mat > pos_lst;
vector< Mat > full_neg_lst;
vector< Mat > neg_lst;
vector< Mat > gradient_lst;
vector< int > labels;
string pos_dir = parser.get<string>("C:/Train_HOG/Franksye_dataset/Train/pos/");
string pos = parser.get<string>("Train/pos");
string neg_dir = parser.get<string>("C:/Train_HOG/Franksye_dataset/Train/neg/");
string neg = parser.get<string>("Train/neg");
if( pos_dir.empty() || pos.empty() || neg_dir.empty() || neg.empty() )
{
cout << "Wrong number of parameters." << endl
<< "Usage: " << argv[0] << " --pd=pos_dir -p=pos.lst --nd=neg_dir -n=neg.lst" << endl
<< "example: " << argv[0] << " --pd=/INRIA_dataset/ -p=Train/pos.lst --nd=/INRIA_dataset/ -n=Train/neg.lst" << endl;
exit( -1 );
}
load_images( pos_dir, pos, pos_lst );
labels.assign( pos_lst.size(), +1 );
const unsigned int old = (unsigned int)labels.size();
load_images( neg_dir, neg, full_neg_lst );
sample_neg( full_neg_lst, neg_lst, Size( 96,160 ) );
labels.insert( labels.end(), neg_lst.size(), -1 );
CV_Assert( old < labels.size() );
compute_hog( pos_lst, gradient_lst, Size( 96, 160 ) );
compute_hog( neg_lst, gradient_lst, Size( 96, 160 ) );
train_svm( gradient_lst, labels );
test_it( Size( 96, 160 ) ); // change with your parameters
return 0;
}
const string & prefix
,这个参数是什么?你能告诉我一件事吗?
使用mingw
时出错$ train_hog.exe
OpenCV Error: Bad argument (undeclared key 'C:/Train_HOG/Franksye_dataset/Train/pos/' requested) in getByName, file C:\opencv\sources\module
s\core\src\command_line_parser.cpp, line 136
terminate called after throwing an instance of 'cv::Exception'
what(): C:\opencv\sources\modules\core\src\command_line_parser.cpp:136: error: (-5) undeclared key 'C:/Train_HOG/Franksye_dataset/Train/p
os/' requested in function getByName
答案 0 :(得分:0)
我解决了问题。这是使用cv::CommandLineParser
..
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv, "{help h|| show help message}"
"{@pd|C:/Train_HOG/Franksye_dataset/|pos_dir}{@p|Train/pos.lst|pos.lst}{@nd|C:/Train_HOG/Franksye_dataset/|neg_dir}{@n|Train/neg.lst|neg.lst}");
if (parser.has("help"))
{
parser.printMessage();
exit(0);
}
vector< Mat > pos_lst;
vector< Mat > full_neg_lst;
vector< Mat > neg_lst;
vector< Mat > gradient_lst;
vector< int > labels;
string pos_dir = parser.get<string>("@pd");
string pos = parser.get<string>("@p");
string neg_dir = parser.get<string>("@nd");
string neg = parser.get<string>("@n");
if( pos_dir.empty() || pos.empty() || neg_dir.empty() || neg.empty() )
{
cout << "Wrong number of parameters." << endl
<< "Usage: " << argv[0] << " --pd=pos_dir -p=pos.lst --nd=neg_dir -n=neg.lst" << endl
<< "example: " << argv[0] << " --pd=/INRIA_dataset/ -p=Train/pos.lst --nd=/INRIA_dataset/ -n=Train/neg.lst" << endl;
exit( -1 );