OpenCV : 训练 SVM 错误 - 断言失败



我编写程序,使用 SVM 和 BoW 对对象进行分类。当我尝试使用 TrainData::create() 成员函数创建训练 SVM 分类器所需的数据时,我收到以下错误。

OpenCV 错误:断言失败 (responses.type() == CV_32F || responses.type() == CV_32S) in setData

这是我从控制器读取训练数据的函数,计算每个列车图像的 BoW 直方图,在矩阵中创建所有列车图像的所有描述符的矩阵,并创建训练数据、标签,然后训练 SVM

void trainClassifier(string dictionaryPath, string trainDataPath, string saveClassifierPath, int samples){
//Write file
FileStorage readFile(dictionaryPath, FileStorage::READ);
//Load into Dictionary matrix
readFile["Data"] >> dictionary;
if(dictionary.empty() == false)
{
    cout << "Error loading visual vocalbulary" << endl;
}
//Set the Bow descripter with the dictionary
testBOW.setVocabulary(dictionary);
//Inititate variables
vector<KeyPoint> keypointTrain;
vector<DMatch> matchTrain;
Mat descriptorTrain;
//inputTrain -> input images, inputFeatures -> BoW descriptor output
Mat inputTrain;
Mat inputFeatures;
//Label array
vector<string> label;
//Create a string to read files from directory
string updatedDataPath;
for(int i = 1; i <= samples; i++)
{
    //Update the string updateDataPath to correspond the image FILENAME with each iteration
    updatedDataPath.append(trainDataPath);
    updatedDataPath += to_string(i);
    updatedDataPath.append(".JPEG");
    //Read FILE from the updated datapath
    inputTrain = imread(updatedDataPath);
    //Convert to single channel, since classifier takes only single channel data
    cvtColor(inputTrain, inputTrain, CV_BGR2GRAY);
    //Generate BoW features/histogram for the train image
    testBOW.compute(inputTrain, keypointTrain, inputFeatures);
    //Load the data in the descriptor Matrix
    descriptorTrain.push_back(inputFeatures);
    //Generate label according to the sample
    if(samples > 1 && samples <= 10)
    {
        label.push_back("OBJ1 POSSITIVE");
    }
    else if (samples > 11 && samples <= 20)
    {
        label.push_back("OBJ1 NEGATIVE");
    }
    //Reset data path
    updatedDataPath.clear();
}
//Convert the descriptor matrix into 32-pt float to make it compatible with classifier
if(descriptorTrain.type() != CV_32F)
{
    descriptorTrain.convertTo(descriptorTrain, CV_32F);
}
//Create train data using TrainData::create()
Ptr<TrainData> trainData = TrainData::create(descriptorTrain, ROW_SAMPLE, label);
//Iniitialize Support vector based classifier (SVM) to classify and detect object
Ptr<SVM>SVM = SVM::create();
SVM->setType(SVM::C_SVC);
SVM->setKernel(SVM::LINEAR);
SVM->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 1e-6));
//Now train the SVM
SVM->trainAuto(trainData);
SVM->save(saveClassifierPath);
cout << "Classifier training status: SUCCESSFUL" << endl;}

任何帮助,不胜感激。感谢和欢呼:)

您使用vector<string>作为 TrainData 响应。

//Label array
vector<string> label;
// [long code]
//Create train data using TrainData::create()
Ptr<TrainData> trainData = TrainData::create(descriptorTrain, ROW_SAMPLE, label);

它应该是 Mat CV_32FCV_32S ,正如错误所说。

您可以在以下位置确认:

  • 文档: 训练数据::创建(...)
  • 源代码:TrainData::create(...) 调用 setData(...)

相关内容

最新更新