1 | initial version |
SVM predict code is, finally, I will determine class by predict value.
int svmPredict(const char* classifier, const char* vocaname, const char* query, const char* method)
{
// load image
cv::Mat img = cv::imread(query, CV_LOAD_IMAGE_GRAYSCALE);
// load svm
cv::SVM svm;
svm.load(classifier);
// declare BOWImgDescriptorExtractor
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("FlannBased");
cv::Ptr<cv::DescriptorExtractor> extractor = new cv::SurfDescriptorExtractor();
cv::BOWImgDescriptorExtractor dextract( extractor, matcher );
// load vocabulary data
cv::Mat vocabulary;
cv::FileStorage fs( vocaname, cv::FileStorage::READ);
fs["vocabulary data"] >> vocabulary;
fs.release();
if( vocabulary.empty() ) return 1;
// Set the vocabulary
dextract.setVocabulary( vocabulary );
std::vector<cv::KeyPoint> keypoints;
detector.detect( img, keypoints);
cv::Mat desc_bow;
dextract.compute( img, keypoints, desc_bow );
if( desc_bow.empty() ) return 1;
// svm predict
float predict = svm.predict(centroids, true);
std::cout << predict << std::endl;
return 0;
}