1

I'd like to implement Bayes and SVM classifiers in OpenCV 3.3. I've written following code for 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;
}

But I've got following exception error on SVM_Model->trainAuto(trainingData).

Unhandled exception at 0x755B5608 in SVMimplemantation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00D8DF58.

And also about Bayes classifier, I've written following code:

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;
}

About this case, I've got following Exception on bayes->train(trainData) too.

Unhandled exception at 0x755B5608 in BayesImplementation.exe: Microsoft C++ exception: cv::Exception at memory location 0x00A5D79C.

To be able to compile the project, Ive uploaded my dataset here. What's the problem and how to fix it?

2
  • Have you checked all your pointers? All the create functions that return Ptr<> will return false on failure. Check after each create with something like if (!trainData) throw("Bad pointer"); Edit: That is, they won't return false, but the Ptr<> class has a bool conversion so you can check if it's valid. Commented Mar 8, 2018 at 7:59
  • @Zebrafish thanks, I've checked it. but the problem is somewhere else! Commented Mar 8, 2018 at 8:37

1 Answer 1

1

Problem is the type of train_label. Try with following:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.