编译train_hog.cpp示例 [英] Compile train_hog.cpp sample
本文介绍了编译train_hog.cpp示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为动物创建自定义HOG检测器。我想编译 train_hog.cpp
样品从OpenCV 3.1.But我有一个问题目录。
这是一个代码 load_images
I want to create custom HOG detector for animal.So I want to compile train_hog.cpp
sample from OpenCV 3.1.But I have a problem about directory.
This is a code for 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&前缀
,这是什么参数?你能告诉我吗?
const string & prefix
, What is this parameter ? Can you tell me about it ?
这是一个错误使用mingw
And this is an error using with 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
推荐答案
我解决了问题。这是一个使用 cv :: CommandLineParser
..
I Solved the Problem.This is a code for using 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 );
这篇关于编译train_hog.cpp示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文