OpenCV SVM - How to change Mat object to appropriate dimensions?

95 views Asked by At

So I am trying to build an SVM for Image classification and have successfully trained the SVM. Now the next step is to use the predict function to get the results. However when I tend to do this an Assertion Error comes up which is as follows:

OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in predict

As I understand this is due to the fact that my sample matrix which is passed as an argument to the predict function is not of the appropriate size. I did a svm->getVarCount() to learn that the number of variables expected is 2. That means the number of columns in my matrix which is passed should be 2. However I am unable to complete this final step with the following code I have written:

Ptr<SVM> svm = SVM::create();
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setGamma(3);
svm->setDegree(3);
svm->train(trainDataFormatted);
svm->save(saveFile);
Mat sampleMat_temp = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
Mat sampleMat;
sampleMat_temp.convertTo(sampleMat,CV_32F);
//Throws an error here because of the mismatch in dimensions
//float response = svm->predict(sampleMat_temp);
std::cout << "Result:" << svm->getVarCount() << std::endl;
std::cout << "Cols:" << sampleMat.cols;
svm->predict(sampleMat);

Here the 'svm' variable has been trained. I tried using reshape on sampleMat but to no avail. Any help would be much appreciated. Thanks!

0

There are 0 answers