Ask Your Question

OpenCV 3.1 (C++) - SVM.load() does not set var_count correctly

asked 2016-09-26 23:45:59 -0500

wacky gravatar image

Okay, I have some training data with 35 variables, I trained an SVM classifier and saved it using:


In another application, I was trying to load the classifier using the XML file saved previously, and I encountered an Assertion Failure when I tried to call predict() on a piece of new input data:

OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in cv::ml::SVMImpl::predict, file E:\Downloads\opencv\sources\modules\ml\src\svm.cpp, line 1930

I double checked that I have loaded my data as CV_32F type, which means that the assertion failed at samples.cols == var_count. And I double checked that I did have the right number of columns (35) in my test data.

So I tried to print out the var_count as follows:

cout << svm->getVarCount() << endl;

The result is that getVarCount() reports value of -842150451, which is clearly wrong. Since my XML does mention var_count as 35. Apparently load() doesn't update this value, causing the assertion.

<?xml version="1.0"?>
  <class_labels type_id="opencv-matrix">
      1 2 3 4 5 6</data></class_labels>
(snipped away rest of XML)

Any solutions to this?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-09-26 23:50:54 -0500

berak gravatar image

SVM::load() is a static function, which returns a new instance.

you have to use it like:

Ptr<SVM> svm = Algorithm::load<SVM>("trained-svm.xml");

in your case, the new instance was simply discarded, and you're querying an uninitialized svm instance for var_count, etc.

edit flag offensive delete link more


thanks... that works..!

wacky gravatar imagewacky ( 2016-09-27 21:49:38 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-09-26 23:45:59 -0500

Seen: 3,898 times

Last updated: Sep 26 '16