我尝试使用OpenCV和SVM进行培训。 但我有一个问题,恰恰是这个错误:
OpenCV Error: Bad argument (train data must be floating-point matrix) in cvCheckTrainData
我必须在图像数据集中进行训练,其中每个图像都有68点(X,Y),我用它来做SVM。
一开始这是我的代码:
//for each image
fin_land.open(str_app); assert(fin_land);
for (int i=(0+(68*count_FOR)); i<(num_landCKplus+(68*count_FOR)); i++) {
fin_land >> f1;
fin_land >> f2;
int data[2] = {(int)f1, (int)f2};
Mat actual(1, 2, CV_32FC1, &data);
trainData.push_back(actual);
}
// SVM
CvSVMParams params;
params.svm_type = CvSVM::NU_SVC;
params.kernel_type = CvSVM::POLY;
trainData = trainData.reshape(1, #numImage);
SVM.train(trainData, trainLabels, Mat(), Mat(), params);
这段代码的问题在于我想用一个Mat来测试68行和2列,因为我的SVM中的每个训练班都有2列,但是我收到了这个错误:
OpenCV Error: Incorrect size of input array (Input sample must be 1-dimensional vector) in cvPreparePredictData
如果我已经正确理解,问题是测试垫的尺寸只需要是一维的。所以,我想像这样修改我的代码:
//for each image
fin_land.open(str_app); assert(fin_land);
for (int i=(0+(68*count_FOR)); i<(num_landCKplus+(68*count_FOR)); i++) {
fin_land >> f1;
fin_land >> f1;
int data = (int)f1;
trainData.push_back(&data);
data = (int)f2;
trainData.push_back(&data);
}
现在每个培训班只有一列,所以即使是测试的Mat,但我有一个新错误,它说:
OpenCV Error: Bad argument (train data must be floating-point matrix) in cvCheckTrainData
问题是trainingSet的新mat的类型是错误的? 我不知道如何修复它......
答案 0 :(得分:0)
float f1,f2;
for (int i=(0+(68*count_FOR)); i<(num_landCKplus+(68*count_FOR)); i++) {
fin_land >> f1;
fin_land >> f1;
trainData.push_back(f1); // pushing the 1st thing will determine the type of trainData
trainData.push_back(f2);
}
trainData = trainData.reshape(1, numItems);
SVM.train(trainData, trainLabels, Mat(), Mat(), params);