gpt4 book ai didi

opencv - 用于训练机器学习算法的矩阵未处理的异常

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:22 25 4
gpt4 key购买 nike

我正在尝试使用 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");

//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;
}

最佳答案

我设法弄明白了,问题出在这里:float label = 1.0; 因为所有被训练的图像不能有相同的标签。系统必须能够区分给定的图像,因此最好将图像分组并赋予组浮点值。

关于opencv - 用于训练机器学习算法的矩阵未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13779981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com