0
我想爲動物創建自定義HOG檢測器。所以我想從OpenCV 3.1編譯train_hog.cpp
示例。但是我有關於目錄的問題。 這是load_images
編譯train_hog.cpp示例
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
***常量字符串及前綴,這是什麼參數?***它可以用於路徑,或者如果您的文件都在默認文件夾中,則可以將其保留爲空。 – drescherjm
這是什麼意思@drescherjm?例如dir = C:\ Train \ pos \ 787.png –
然後爲'const string&filename'做前綴'C:/ Train/pos /' – drescherjm