OpenCV : 训练 SVM 错误 - 断言失败
OpenCV : Training SVM Error - Assertion failed
我编写程序,使用 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_32F
或CV_32S
,正如错误所说。
您可以在以下位置确认:
- 文档: 训练数据::创建(...)
- 源代码:TrainData::create(...) 调用 setData(...)
相关文章:
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 在 CppUnit 中测试中止断言失败
- 使用扫描的调试断言失败
- 为什么我的Qt程序在断言失败后继续运行?
- 图片不显示,关闭时出错 --> 调试断言失败!表达式:is_block_type_valid(标头>_block_use)
- 访问提升:shared_ptr 主范围外崩溃,断言失败:px != 0.指针的正确用法是什么?
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 错误:断言失败 (src.type() == CV_8UC1) 在阈值中
- Boost Beast 异步服务器失败,断言失败:(id_ != T::id) 在多个 aync 调用中
- 使用imwrite OpenCV时断言失败
- 调试断言失败的缓冲区!=nullptr
- 使用 ofstream 写入文本文件时断言失败
- 提升继续恢复断言失败
- 获取有关调试断言失败的错误:表达式:"(_Ptr_user &(_BIG_ALLOCATION_ALIGNMENT -1)) == 0" &&0
- OpenCV(3.4.1) 错误:断言失败(变暗 <= 2 && step[0] > 0) in cv::Mat::locateROI
- 在Win10上使用带有Qt5的OpenCV3.0,调试断言失败
- MFC GetDC() 断言失败 - "Not a window"
- OpenCV 错误:断言失败 - 矩阵减去