属于OpenCV 3.1和Qt-Creator的实际问题。根据{{3}}报告,我试图在我自己的项目中实施。
如果我想训练我的SVM,我会得到以下错误:
OpenCV错误:断言失败(samples.type()== CV_32F || samples.type()== CV_32S)在setData中,文件C:... \ opencv-master \ modules \ ml \ src \ data。 cpp,第259行
我的部分代码如下所示。
int num_files = 205;
int img_area = 55*66;
Mat training_mat(num_files,img_area, CV_32FC1);
vector<int> label;
//Labels: 1-138 == 1 | 139-205 == 0
for(int i = 0; i < 139; i++)
{
label.push_back(1);
}
for(int i = 139; i < 206; i++)
{
label.push_back(0);
}
Mat classes;
Mat(label).copyTo(classes);
// Imread Pictures
for(int c = 1; c < num_files; c++)
{
ostringstream name;
name << "../Name_" << c << ".png";
img_mat = imread(name.str(),0);
int ii = 0;
for (int i = 0; i < img_mat.rows; i++)
{
for (int j = 0; j < img_mat.cols; j++)
{
training_mat.at<float>(c,ii++) = (float)img_mat.at<uchar>(i,j);
}
}
}
training_mat.convertTo(training_mat, CV_32FC1);
我将数据写入带有以下参数的.xml文件:
FileStorage fs("train_SVM.xml", FileStorage::WRITE);
fs << "ImagesCount" << num_files;
time_t rawtime; time(&rawtime);
fs << "createDate" << asctime(localtime(&rawtime));
fs << "TrainingData" << training_mat;
fs << "classes" << classes;
fs.release();
使用以下代码训练SVM:
//Read file storage.
FileStorage fs;
fs.open("train_SVM.xml", FileStorage::READ);
Mat SVM_TrainingData;
Mat SVM_Classes;
fs["TrainingData"] >> SVM_TrainingData;
fs["classes"] >> SVM_Classes;
Ptr<ml::SVM> svm = ml::SVM::create();
//Set SVM params
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::LINEAR);
svm->setDegree(0);
svm->setGamma(1);
svm->setCoef0(0);
svm->setC(1);
svm->setNu(0);
svm->setP(0);
svm->setTermCriteria(TermCriteria(CV_TERMCRIT_ITER, 1000, 0.01));
//Train SVM
svm->train( SVM_TrainingData, ml::ROW_SAMPLE , SVM_Classes );
你知道具体问题是什么吗?