我有vector<vector<float> >
大小 1800 * 160 的功能现在我需要火车svm,我尝试使用OPENCV SVM但是在调试模式下svm-&gt;火车返回false并且在释放模式这个例子提出:
Exception thrown at 0x00007FFF587AC387 (vcruntime140.dll):Access violation reading location 0x00000048B7FED000.
我的代码:
void Classifier::trainSVM(vector<vector<float> > data,cv::Mat Lable)
{
// Train the SVM
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::LINEAR);
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 50000, 1e-6));
cv::Mat trainingData = cv::Mat(data.size(), 160, CV_32FC1, data.data());
std::cout << "\nBegan Training Svm in vector faces.";
bool trained = svm->train(trainingData, cv::ml::ROW_SAMPLE, Lable);
if (trained)
svm->save("svm_data.xml");
std::cout << "\nEnd Training Svm in vector faces.";
}
答案 0 :(得分:0)
感谢berak的solution
你的vector<vector<float>>
是罪魁祸首,opencv的机器学习期望单个Mat具有连续数据。
你需要将它复制到Mat中,每个向量都在一行上,可能是这样的:
vector<vector<float>> vf {{1,2,3},{4,5,6},{7,8,9}}; // demo data
Mat data;
for ( auto v : vf ) {
Mat row = Mat(v, true).reshape(1,1); // deep copy, reshape to row
data.push_back(row);
}
cerr << data << endl;
[1, 2, 3;
4, 5, 6;
7, 8, 9]