我想在Bayes中实现SVMOpenCV 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)遵循了Exception。
Unhandled exception at 0x755B5608 in BayesImplementation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00A5D79C.

为了能够编译项目,我已经上传了我的数据集here。有什么问题以及如何解决?

最佳答案

问题是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);

10-07 18:19