I am trying to train SVM classifier using SIFT feature and Bag Of Word (BOW). But my program is not running, but it is showing no error in my code. I am not able to figure out my faults. Can anybody give me any suggestion? Thanks in advance. Here is a piece of code by which I have declared variables and SVM classifier parameter.
Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
std::vector<KeyPoint> keypoints;
Mat desc;
//defining terms for bowkmeans trainer
int cluster = 5;
int retries = 1;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.00001);
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(cluster, tc, retries, flags);
Mat vocubulary;
BOWImgDescriptorExtractor bowDE(descriptor, matcher);
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
Mat features1, features2;
Mat bowDescriptor, bowDescriptor2;
int dictionarySize=100 ;
vector<string>names;
string folder= argv[1];
glob(folder, names);
Mat trainme(0, dictionarySize, CV_32FC1);
Mat labels(0, 1, CV_32FC1);
By the following part I tried to obtain features from images.
for(unsigned int i =0; i<names.size();i++)
{
Mat img = imread( names[i], CV_LOAD_IMAGE_GRAYSCALE );
if(!img.data)
{ cout<< " --(!) Error reading images " << endl; return -1; }
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<<"Program is running"<<endl;
}
By the following part, I trained SVM classifier
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
CvSVM SVM;
SVM.train(trainme,labels);
SVM.save("classifier.xml");
Mat tryme(0, dictionarySize, CV_32FC1);
Mat tryDescriptor;
Mat test = imread("C:\\Users\\ASHRAF\\Desktop\\Oslo\\ResizedSamples\\24.bmp", 0);
vector<KeyPoint> keypoints3;
features->detect(test, keypoints3);
bowDE.compute(test, keypoints3, tryDescriptor);
tryme.push_back(tryDescriptor);
cout<<SVM.predict(tryme)<<endl;
}