OpenCV SVM在火车上抛出异常,"坏参数(只有一个类)"

tuc*_*uck 5 c++ opencv computer-vision surf sift

我坚持这个.

我试图通过OpenCV功能2d框架进行一些对象分类,但是在训练我的SVM时遇到了麻烦.

我能够使用BowKMeansTrainer提取词汇表并对它们进行聚类,但在我从训练数据中提取要添加到训练器并运行SVM.train方法之后,我得到以下异常.

OpenCV Error: Bad argument (There is only a single class) in     cvPreprocessCategoricalResponses, file /home/tbu/prog/OpenCV-2.4.2/modules/ml/src    /inner_functions.cpp, line 729
terminate called after throwing an instance of 'cv::Exception'
 what():  /home/tbuchy/prog/OpenCV-2.4.2/modules/ml/src/inner_functions.cpp:729: error:     (-5) There is only a single class in function cvPreprocessCategoricalResponses
Run Code Online (Sandbox Code Playgroud)

我尝试使用不同的训练器来修改字典大小,确保我的矩阵类型是正确的(尽我所能,仍然是opencv的新手).

有没有看到这个错误或有任何关于如何解决它的见解?

我的代码看起来像这样:

trainingPaths = getFilePaths();
extractTrainingVocab(trainingPaths);
cout<<"Clustering..."<<endl;
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);


Mat trainingData(0, dictionarySize, CV_32FC1);
Mat labels(0, 1, CV_32FC1);
extractBOWDescriptor(trainingPaths, trainingData, labels);


//making the classifier
CvSVM classifier;
CvSVMParams params;
params.svm_type    = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit   = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

classifier.train(trainingData, labels, Mat(), Mat(), params);
Run Code Online (Sandbox Code Playgroud)

sol*_*les 10

根据错误,您看起来labels只包含一类数据.也就是说,您的所有功能trainingData都具有相同的标签.

例如,假设您正在尝试使用SVM来确定图像是否包含猫.如果每个条目labels都相同,那么要么......

  • 你所有的训练图像都标有"是这是一只猫"
  • 或者,所有训练图像都标记为"不,这不是猫".

SVM尝试分离两个(或有时更多)数据类,因此如果您只提供一类数据,SVM库会抱怨.

要查看这是否是问题,我建议添加一个print语句来检查是否labels只包含一个类别.这是一些代码来执行此操作:

//check: are the printouts all the same?
for(int i=0; i<labels.rows; i++)
    for(int j=0; j<labels.cols; j++)
        printf("labels(%d, %d) = %f \n", i, j, labels.at<float>(i,j));
Run Code Online (Sandbox Code Playgroud)

一旦你extractBOWDescriptor()加载数据labels,我假设它labels的大小(trainingData.rows, trainingData.cols).如果没有,这可能是一个问题.