好的,我正在尝试使用BoW训练SVM。我在互联网上找到了一些代码,但无法找到一些解释。现在我有这个:
string to_string(const int val) {
int i = val;
std::string s;
std::stringstream out;
out << i;
s = out.str();
return s;
}
int main() {
initModule_nonfree();
Ptr<FeatureDetector> detector = FeatureDetector::create("SURF");
Ptr<DescriptorExtractor> descriptors = DescriptorExtractor::create("SURF");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
TermCriteria tc(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 10, 0.001);
int dictionarySize = 1000;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
BOWImgDescriptorExtractor bowDE(descriptors, matcher);
string dir = "./positive_large", filepath;
DIR *dp;
struct dirent *dirp;
struct stat filestat;
dp = opendir( dir.c_str() );
Mat features;
Mat img;
vector<KeyPoint> keypoints;
int i = 0;
while(dirp = readdir(dp)) {
filepath = dir + "/" + dirp->d_name;
cout << "reading: " << filepath << endl;
if (stat( filepath.c_str(), &filestat )) continue;
if (S_ISDIR( filestat.st_mode )) continue;
img = imread(filepath, 0);
detector->detect(img, keypoints);
KeyPointsFilter::retainBest(keypoints, 1000);
descriptors->compute(img, keypoints, features);
bowTrainer.add(features);
drawKeypoints(img, keypoints, img);
string filenum = to_string(i);
string filename = "./detected_features/" + filenum + ".png";
cout << "filename: " << filename << endl;
imwrite(filename, img);
i++;
}
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
return 0;
}
所以我的问题是有人可以向我解释使用什么标签以及如何使用它们?我想我有一些想法,但一个好的解释将是非常有帮助的。我是否必须为每个阴性样本加上零,为每个阳性样本加一个?当我发布代码时,我要做的下一步是什么?提前谢谢你:)