[英]OpenCV : Training SVM Error - Assertion failed
Im writing program to classify objects using SVM and BoW. 我正在编写程序来使用SVM和BoW对对象进行分类。 I am getting the following error when I try use the TrainData::create() member function to create the data necessary to train SVM classifier.
尝试使用TrainData :: create()成员函数创建训练SVM分类器所需的数据时,出现以下错误。
OpenCV Error: Assertion failed (responses.type() == CV_32F || responses.type() == CV_32S) in setData
OpenCV错误:setData中的断言失败(responses.type()== CV_32F || response.type()== CV_32S)
This is my function to read the train data from a director, compute BoW histogram for each train image, create a matrix of all descriptors of all train images in a matrix and the create the train data, labels and then train the SVM 这是我的功能,用于从导演那里读取火车数据,为每个火车图像计算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;}
Any help is appreciated. 任何帮助表示赞赏。 Thanks and cheers :)
谢谢和欢呼:)
You are using a vector<string>
as the TrainData responses. 您正在使用
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);
And it shoud be a Mat
CV_32F
or CV_32S
, as the error says. 如错误
CV_32S
,它应该是Mat
CV_32F
或CV_32S
。
You can confirm that at: 您可以通过以下方式确认:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.