我想在Bayes
中实施SVM
和OpenCV 3.3
个分类器。我已经为SVM
编写了以下代码:
int main()
{
// Load data
FileStorage fs("newStorageFile.yml", FileStorage::READ);
// Read data
Mat test_data, test_labels, enrol_data, enrol_labels;
Mat train_labels = Mat::zeros(650, 1, CV_32S);
Mat train_data = Mat::zeros(650, 600, CV_32S);
fs["train_data"] >> train_data;
fs["train_labels"] >> train_labels;
fs["test_data"] >> test_data;
fs["test_labels"] >> test_labels;
fs["enrol_data"] >> enrol_data;
fs["enrol_labels"] >> enrol_labels;
Ptr<ml::SVM> SVM_Model = ml::SVM::create();
SVM_Model->setType(ml::SVM::C_SVC);
SVM_Model->setKernel(ml::SVM::RBF);
Ptr<ml::TrainData> trainingData = ml::TrainData::create(train_data, ml::SampleTypes::ROW_SAMPLE, train_labels);
SVM_Model->trainAuto(trainingData);
return 0;
}
但我在SVM_Model->trainAuto(trainingData)
上遇到异常错误。
Unhandled exception at 0x755B5608 in SVMimplemantation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00D8DF58.
关于Bayes
分类器,我写了以下代码:
int main()
{
// Load data
FileStorage fs("newStorageFile.yml", FileStorage::READ);
// Read data
Mat test_data, test_labels, enrol_data, enrol_labels;
Mat train_labels = Mat::zeros(650, 1, CV_32F);
Mat train_data = Mat::zeros(650, 600, CV_32F);
fs["train_data"] >> train_data;
fs["train_labels"] >> train_labels;
fs["test_data"] >> test_data;
fs["test_labels"] >> test_labels;
fs["enrol_data"] >> enrol_data;
fs["enrol_labels"] >> enrol_labels;
Ptr<ml::NormalBayesClassifier> bayes = ml::NormalBayesClassifier::create();
Ptr<ml::TrainData> trainData = ml::TrainData::create(train_data, ml::SampleTypes::ROW_SAMPLE, train_labels);
bayes->train(trainData);
Mat output, outputProb;
bayes->predictProb(test_data, output, outputProb);
return 0;
}
关于这种情况,我在bayes->train(trainData)
上也有以下异常。
Unhandled exception at 0x755B5608 in BayesImplementation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00A5D79C.
为了能够编译项目,我上传了我的数据集here。问题是什么以及如何解决?
答案 0 :(得分:0)
问题是train_label
的类型。尝试以下:
Mat train_labels32S = Mat::zeros(train_labels.rows, 1, CV_32S);
for (int i = 0; i < train_labels.rows; i++)
train_labels32S.at<int>(i, 0) = train_labels.at<int>(i, 0);
//some code
Ptr<ml::TrainData> trainingData = ml::TrainData::create(train_data, ml::SampleTypes::ROW_SAMPLE, train_labels32S);