I am trying to do 3-class classification using SVM. For that, i am preparing vocabulary during the SVM training. But as i am getting random results during SVM prediction, so i suspect that there is some problem in my vocabulary creation method. My code to create vocabulary is following:
//Mat train --- it should contain the feature vectors
//Mat response-- it will contain the class labels
void svm::createTrainingDateUsingBOW(int flag,Mat& train, Mat& response, int label)
{
int cluster = 9; // Common for all classes
cv::Mat imageForTraining;
std::vector<cv::KeyPoint> keypoints;
cv::SurfFeatureDetector detector(500);
cv::Ptr<cv::DescriptorExtractor> cvDescExt = new cv::SurfDescriptorExtractor();
cv::Mat descriptors;
cv::BOWKMeansTrainer bow(cluster, cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, FLT_EPSILON), 1, cv::KMEANS_PP_CENTERS);
cv::Mat vocabulary;
if (flag == 1)
{
for(int i=1; i<=400; i++)
{
counter++;
cout<<"\n counter: "<<counter;
char filepath[255];
sprintf(filepath, "class1/%d.JPG",i); // we need class1/1.JPG etc
imageForTraining = cv::imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);
// Preparing keypoints using detector
detector.detect(imageForTraining, keypoints);
// now getting the DESCRIPTORS for the given keypoints
cvDescExt->compute(imageForTraining, keypoints, descriptors);
//BOW
if(keypoints.size() > cluster) // so that (N<k) error won't come
{
if (!descriptors.empty()) bow.add(descriptors);
//VOCABULARY
vocabulary = bow.cluster();
cv::Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
cv::Ptr<DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
cv::BOWImgDescriptorExtractor descExtractor (extractor, matcher);
descExtractor.setVocabulary(vocabulary);
Mat bowDescriptors;
descExtractor.compute(imageForTraining, keypoints, bowDescriptors);
if ( !bowDescriptors.empty())
{
train.push_back(bowDescriptors);
response.push_back(label);
}
}// ending if loop
} // ending For loop
}// ending if(flag ) loop
if (flag == 2)
{
for(int i=1; i<=400; i++)
{
counter++;
cout<<"\n counter: "<<counter;
char filepath[255];
sprintf(filepath, "class2/%d.JPG",i); // we need class1/1.JPG etc
imageForTraining = cv::imread(filepath, CV_LOAD_IMAGE_GRAYSCALE);
// Preparing keypoints using detector
detector.detect(imageForTraining, keypoints);
// now getting the DESCRIPTORS for the given keypoints
cvDescExt->compute(imageForTraining, keypoints, descriptors);
//BOW
if(keypoints.size() > cluster) // so that (N<k) error won't come
{
if (!descriptors.empty()) bow.add(descriptors);
//VOCABULARY
vocabulary = bow.cluster();
cv::Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
cv::Ptr<DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
cv::BOWImgDescriptorExtractor descExtractor (extractor, matcher);
descExtractor.setVocabulary(vocabulary);
Mat bowDescriptors;
descExtractor.compute(imageForTraining, keypoints, bowDescriptors);
if ( !bowDescriptors.empty())
{
train.push_back(bowDescriptors);
response.push_back(label);
}
}// ending if loop
} // ending For loop
}// ending if(flag ) loop
....Similary for Class-3
}