Hello, I'm trying to implement a BoW + SVM method for classification that takes several images for class one and two and then classifies the query image based on those. For some reason after constructing bow descriptor, it returns an empty descriptor vector thus I can't train the SVM let alone classify. Relevant part of my code is below, I'd appreciate any insight. Thanks a lot.
.....
while (dirp = readdir( dp ))
{
filepath = dir + "/" + dirp->d_name;
// If the file is a directory (or is in some way invalid) we'll skip it
if (stat( filepath.c_str(), &filestat )) continue;
if (S_ISDIR( filestat.st_mode )) continue;
Mat img = imread(filepath);
if (!img.data) {
cout <<"Can't open file." << endl;
continue;
}
features->detect(img, keypoints);
descriptor->compute(img, keypoints, features1);
bowDE.compute(img, keypoints, bowDescriptor);
//trainme.push_back(bowDescriptor);
float label = 1.0;
labels.push_back(label);
bowTrainer.add(features1);
cout << "." << endl;
}
while (dirp2 = readdir( dp2 ))
{
filepath2 = dir2 + "/" + dirp2->d_name;
// If the file is a directory (or is in some way invalid) we'll skip it
if (stat( filepath2.c_str(), &filestat2 )) continue;
if (S_ISDIR( filestat2.st_mode )) continue;
Mat img2 = imread(filepath2);
if (!img2.data) {
cout <<"Can't open file." << endl;
continue;
}
features->detect(img2, keypoints2);
descriptor->compute(img2, keypoints2, features2);
bowDE.compute(img2, keypoints2, bowDescriptor2);
trainme.push_back(bowDescriptor2);
float label = 0.0;
labels.push_back(label);
bowTrainer.add(features2);
cout << "." << endl;
}
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
CvSVM SVM;
SVM.train(trainme,labels);
Mat tryme(0, dictionarySize, CV_32FC1);
Mat tryDescriptor;
Mat img3 = imread("c:\\Users\\Elvan\\Desktop\\frame_0118.jpg", 0);
vector<KeyPoint> keypoints3;
features->detect(img3, keypoints3);
bowDE.compute(img3, keypoints3, tryDescriptor);
tryme.push_back(tryDescriptor);
cout<<SVM.predict(tryme)<<endl;