我正在尝试使用OpenCV实现词袋模型。在创建词典后,我使用NormalBayesClassifier
来训练和预测系统。
我按照文档的要求准备了trainme
矩阵,每个样本位于每一行。但问题是,在这一行classifier.train(trainme, labels);
出现了未处理的异常。
我使用的完整代码如下:
int _tmain(int argc, _TCHAR* argv[]){initModule_nonfree();Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");//定义bowkmeans训练器的参数TermCriteria tc(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 10, 0.001);int dictionarySize = 100;int retries = 1;int flags = KMEANS_PP_CENTERS;BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);BOWImgDescriptorExtractor bowDE(descriptor, matcher);//**创建词典**//Mat features1, features2;Mat img = imread("c:\\1.jpg", 0);Mat img2 = imread("c:\\2.jpg", 0);vector<KeyPoint> keypoints, keypoints2;features->detect(img, keypoints);features->detect(img2,keypoints2);descriptor->compute(img, keypoints, features1);descriptor->compute(img2, keypoints2, features2);bowTrainer.add(features1);bowTrainer.add(features2);Mat dictionary = bowTrainer.cluster();bowDE.setVocabulary(dictionary);//**词典已创建**////**现在训练分类器**//Mat trainme(0, dictionarySize, CV_32FC1); Mat labels(0, 1, CV_32FC1); //1d矩阵,32fc1是normalbayesclassifier类的要求Mat bowDescriptor, bowDescriptor2;bowDE.compute(img, keypoints, bowDescriptor);trainme.push_back(bowDescriptor);float label = 1.0;labels.push_back(label);bowDE.compute(img2, keypoints2, bowDescriptor2);trainme.push_back(bowDescriptor2);labels.push_back(label);NormalBayesClassifier classifier;classifier.train(trainme, labels);//**分类器已训练**////**现在尝试使用相同的训练过的分类器进行预测,应该返回1.0**//Mat tryme(0, dictionarySize, CV_32FC1);Mat tryDescriptor;Mat img3 = imread("2.jpg", 0);vector<KeyPoint> keypoints3;features->detect(img3, keypoints3);bowDE.compute(img3, keypoints3, tryDescriptor);tryme.push_back(tryDescriptor);cout<<classifier.predict(tryme);waitKey(0);return 0;}
回答:
我已经解决了这个问题,问题出在这里:float label = 1.0;
因为所有训练的图像不能有相同的标签。系统必须能够区分给定的图像,因此最好将图像按组排列,并为这些组分配浮点值。