3

我試圖用opencv實現一攬子單詞的方法。在製作字典後,我使用NormalBayesClassifier來訓練和預測系統。未處理的異常在用於訓練機器學習算法的矩陣

我已按照每行中每個樣本的文檔準備了trainme矩陣。但問題是,它提供了一個未處理的異常,在這一行:classifier.train(trainme, labels);

我使用的完整的代碼如下:所有float label = 1.0;

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"); 

//defining terms for bowkmeans trainer 
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); 

//**creating dictionary**// 

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); 

//**dictionary made**// 

//**now training the classifier**// 

Mat trainme(0, dictionarySize, CV_32FC1); 
Mat labels(0, 1, CV_32FC1); //1d matrix with 32fc1 is requirement of normalbayesclassifier class 

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); 

//**classifier trained**// 

//**now trying to predict using the same trained classifier, it should return 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; 
} 
+3

爲什麼這降低了投票率? OP已經嘗試了一些東西,發現了一個錯誤,檢查了文檔,但發現自己被卡住了,並且這發生了。我認爲這足以證明_upvotes_。 – 11684

+0

感謝您的支持,我設法自己解決了這個問題,並在此處添加了答案。 – ipunished

回答

2

我設法搞清楚,這個問題躺在這兒正在訓練的圖像不能有相同的標籤。系統必須能夠區分給定的圖像,因此最好將圖像分組排列,併爲組提供浮點值。

相關問題